# What is the opinion of matlabs "extra rounding improvements"?

2 visualizzazioni (ultimi 30 giorni)
Patrik Ek il 14 Apr 2014
Commentato: Walter Roberson il 14 Apr 2014
Hi, during my work with matlab I have noticed that matlab always tries to improve some basic functionality of other programming languages. Some examples.
a = cast(0.5,'int32') % a = 1
a = cast(-1.5,'uint32') % a = 0
a = int32(1)/int32(2) % a = 1
The division can of course be solved by idivide, but the issues with cast remains. Also this definitely shows that matlab cannot properly do integer operations since the decimals are obviously used by the /.
Is there some way to do a cast that works as it should in matlab? What is the general opinion of matlabs " IMPROVED " behaviour?
##### 0 CommentiMostra -2 commenti meno recentiNascondi -2 commenti meno recenti

Accedi per commentare.

### Risposta accettata

Walter Roberson il 14 Apr 2014
MATLAB uses "saturation" arithmetic when converting to the integer data types. Any value that is larger than the integer type can hold is converted to the largest value that can be held, and any value that is less than the integer type can hold is converted into the smallest value that can be held. As the smallest value that unsigned integer types can hold is 0, negative values get transformed into 0.
If you examine the behavior of other languages such as C, you will find that casting a signed value into an unsigned integer does depend upon sign. If you do not want it to, then take abs() before doing the type conversion.
Whether integer operations round or truncate does not matter much to me; in cases where it matters, I can document and (if needed) I can code around it.
##### 1 CommentoMostra -1 commenti meno recentiNascondi -1 commenti meno recenti
Patrik Ek il 14 Apr 2014
Thanks for the answer. This was kind of what a I was asking.

Accedi per commentare.

### Più risposte (1)

John D'Errico il 14 Apr 2014
Modificato: John D'Errico il 14 Apr 2014
"a = cast(-1.5,'unit32') % a = 0"
So is unit32 a new data type? Maybe a 32 bit scalar, that always takes on the value 1?
I'll assume you simply cannot type, and really meant uint32. Of course, then you did not actually bother to test the code you posted, a no-no.
Are you seriously surprised that when you cast a negative number to a non-negative integer class, that you got zero??????? You think this is a bad thing? What did you expect to get out here? pi?
As far as "improving" functionality, there is no claim to an improvement, merely a behavior that is its own. You learn how a language works. Every language will generally be subtly different, with its own characteristic behavior based on decisions made by those who designed the language. Clearly you disagree with those decisions, while I don't care, as I am not closely wedded to some other language. As far as I can see, once you fully learn to use a tool, you learn its characteristic behavior, and there are no problems. One can always get the answer needed, as long as one knows what to expect.
Sorry for a response that may appear a bit sarcastic, but your question is really more of a rant than a question. When you make claims that MATLAB ALWAYS does something, you are making broad assertions that will not be accurate for any tool as large as MATLAB, and are clearly intended as flame bait. Likewise, you ask for the general opinion of people, when you have made your opinion very clear on the matter.
##### 3 CommentiMostra 1 commento meno recenteNascondi 1 commento meno recente
Patrik Ek il 14 Apr 2014
Well I see your point, should I remove the question?
Walter Roberson il 14 Apr 2014
I suggest leaving it as it might be of interest to others.

Accedi per commentare.

### Categorie

Scopri di più su Logical in Help Center e File Exchange

### Community Treasure Hunt

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

Start Hunting!

Translated by