Arithmetic promotion: floating-point to integer?

Hi,
[quote "MATLAB - Programming Fundamentals R2011b.pdf"] Arithmetic operations that involve both integers and floating-point always result in an integer data type. MATLAB rounds the result, when necessary, according to the default rounding algorithm. The example below yields an exact answer of 1426.75 which MATLAB then rounds to the next highest integer:
int16(325) * 4.39
ans =
1427
[/quote]
In C/C++, the arithmetic promotion rule is to upper ↑: char to int, int unsigned int, int to float, float to double, etc.
Why Matlab designs to be to lower (int)?
Bob

1 comentario

Walter Roberson
Walter Roberson el 13 de Oct. de 2011
The Algol Coercing patents were still in effect at the time MATLAB was designed?

Iniciar sesión para comentar.

 Respuesta aceptada

Mike Hosea
Mike Hosea el 13 de Oct. de 2011

2 votos

I don't know. Since I work in code generation, these sorts of behaviors are a regular nuisance to me, but maybe I can imagine why. In MATLAB, most users do almost everything with doubles. When programming in MATLAB language, the average user doesn't even think about numeric types. I wasn't around then, but I think integers were originally added to the language primarily because it was burdensome to represent things like images as large arrays of doubles. Having added integers to the language to save on storage, you wouldn't want something simple like multiplying a large int8 array by 2 (that's double precision 2.0 in MATLAB) to generate a large double precision array out of a large integer array. I guess they preferred the current behavior to requiring, in that case, multiplication by int8(2). This rationale is, of course, less compelling when we're talking about scalars, but having inconsistent rules for scalars would be madness. -- Mike

12 comentarios

Bob Li
Bob Li el 14 de Oct. de 2011
Mike,
Do you write compiler for Matlab? I heard that earlier versions of Matlab were written in C/C++, but later moved to Java? Are Matlab releases in the form of Java bytecode nowadays, which could run on all JVM-installed platforms, or compiled machine code for different CPU's?
If it is shipped in bytecode, does it uses JIT, or even Sun(Oracle)'s HotSpot compiling?
Bob
Bob Li
Bob Li el 14 de Oct. de 2011
Mike,
I am familiar with C but not Java. I looked at [Matlab Programming Fundamentals 2011b], and the 8 integer data types {signed, unsigned}×{8,16,32,64} as well as single and double floating-point types are all standard C types in terms of range, but there are some differences:
1. When 64-bit integer and double involved, MATLAB uses 80-bit extended precision. This is not in C.
2. In C, the treatment of integer-type overflow/underflow in assignment/expression is by truncating, but in Matlab it is clamping.
Although it might also be that MATLAB is written in Java now, Java's type and arithmetic rule might still be close to C. But the points mentioned above doesn't seem like either C or Java.
So regarding fundamental data types: Are they Objects defined on top of C/Java's native data types? Do they use operators (+,-,*,/) which are overloaded operators for these object types?
Bob
Jan
Jan el 14 de Oct. de 2011
@Bob: No, MATLAB is not written in Java. The graphic interface uses Java code, but not the numerical calculations, string operations, image processing, etc. In former releases of MATLAB a lot of functions have been shipped with their C-Mex-sources. These sources have been very helpful for educational reasons, but today most of them are either shipped in compiled or built-in form. A comparison of the results and runtime reveals, that still the same code is used.
You can use long doubles with 80 bit precision in C, e.g. the LCC and Intel compilers support this.
Bob Li
Bob Li el 14 de Oct. de 2011
Jan,
I see it, thanks.
Mike Hosea
Mike Hosea el 15 de Oct. de 2011
Sounds like Jan's answer helped you, but let me just say that MATLAB language is implemented in software with "just-in-time" compilation in some contexts. It is written to use the hardware intelligently, but integer arithmetic and promotion does what the MATLAB software says it does, not what some other language says it ought to do. It is like doing floating point in software. You can make it do whatever you want, e.g. use BCD instead of binary IEEE. The data itself is stored in the usual number of bits, but the operations that work on it are more complex. Obviously if you're going to eschew the behaviors that are supported most directly by the hardware, there is likely to be a run-time cost for defining your own behavior and not "going with the flow." C'est la vie. In code generation we have options to turn off integer saturation, so if you turn it off (thereby warranting that your algorithm does not rely on it), then we can probably generate the kind of C code that you wanted. This would be more direct if MATLAB would add C-integer types that are defined to wrap and promote like C integers. Alas, they do not exist, but we can dream and cajole.
Bob Li
Bob Li el 16 de Oct. de 2011
Mike,
I got your point. Thanks.
Bob
Bob Li
Bob Li el 16 de Oct. de 2011
Mike,
Is there anyway that you can get notifications of later comments on an answer? I guess Matlab people should have many such comments, and do they (you) track all of them by reloading each threads' pages?
Walter Roberson
Walter Roberson el 16 de Oct. de 2011
Bob, At the main Answers page, by default the posts are listed with the most recent update first. You can look through the list of posts for updates to threads of interest to you, stopping looking when you recognize that the posts are older than the last time you looked at the list.
You can also subscribe to the RSS feed via the "Subscribe to this View" link just below the Answer search box. I do not know if it is possible to subscribe to an individual post as I do not use that facility.
You may wish to vote for or add ideas to the several Wish-List topics that the current setup has generated. See http://www.mathworks.com/matlabcentral/answers/994-wish-list-for-matlab-answer-sections
It is likely that no-one else has made as many Comments as I have; certainly no-one else has posted as many Answers as I have. I simply read (or at least skim) nearly everything new since I last checked: if it is on a topic that I don't know how to solve myself, then I learn something from reading the answers.
Bob Li
Bob Li el 16 de Oct. de 2011
Walter,
Thanks for the answer. I have just voted in the wish-list. Could you help to relay this request to Mathworks? All forums I have used have notification functionality, and it is quite sorry that this is no available on Mathworks forum.
Additionally, is there a way to know if a person is a Mathworks employee by looking at their profile?
Walter Roberson
Walter Roberson el 16 de Oct. de 2011
While I remember: Bob, have you tried using the My Questions link on the main Answers page? That will show you the list of Questions you submitted, most recently modified first. This at least allows you to track the things you have submitted directly, but it is not useful for tracking other topics you might be interested in. The My Answers link can help track questions you submitted an Answer for (but not questions you commented on.)
My mechanism for conveying feature suggestions to Mathworks is to write in to the Wish-List. I personally do not email individuals at Mathworks about feature suggestions (only about serious site problems.) The official mechanism is to write to files@mathworks.com
Mathworks employees used to be marked with the Mathworks icon in their profile, but Mathworks decided to remove that.
I am not now, and never have been, an employee (or contractor) of Mathworks -- I'm just a busy-body ;-)
Bob Li
Bob Li el 17 de Oct. de 2011
Walter,
Thanks for answer. "My questions" page only shows something like:
2answers
0votes
No rich formatting for posts?
Asked 4 days ago by Bob Li
Accepted Answer from Walter Roberson
Tags fomatting post
But I cannot find any information about which one has last been updated?
Walter Roberson
Walter Roberson el 17 de Oct. de 2011
Your My Questions page should (for you) currently list 7 questions. They are ordered from most recently updated (by anyone, including via comments) to least recently updated. What you list above is just the information for one of the questions: there should be 6 above that one.

Iniciar sesión para comentar.

Más respuestas (0)

Preguntada:

el 13 de Oct. de 2011

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by