 |
float expressions
Started by chriss0212 at 01-20-2008 1:06. Topic has 4 replies.
|
|
01-20-2008, 1:06
|
chriss0212
Joined on 03-21-2006
Düsseldorf/ Germany
Posts 297
|
|
|
hi all,
if i do A*360/80 in a float expression i get 4,5
if i do 360/80*A in a float expression i get 4
seems to be a bug??
greetz
chriss
...........and don't give up :-)
|
|
|
|
|
Report
|
|
|
|
01-20-2008, 11:55
|
framefactory
Joined on 01-11-2006
Zurich, Switzerland
Posts 113
|
|
|
No bug, that's all correct. I suppose that A is a float variable. So as soon as A is involved in the expression, it evaluates as a float expression, even if you use Int constants. Standard math expressions are evaluated from left to right (unless you use brackets to specify otherwise).
In the first version, A is first multiplied by 360 then the product is divided by 80. Therefore, everything is evaluated as float. In the second case, 360 is first divided by 80. Both numbers are Integers, so the result is an Integer as well (360/80 = 4, not 4.5).
What you should always do in float expressions to avoid such ambiguous behavior: Enforce all constants to be treated/evaluated as floating point numbers: A * 360.0f / 80.0f
Cheers, Ralph
|
|
|
|
|
Report
|
|
|
|
01-20-2008, 23:58
|
chriss0212
Joined on 03-21-2006
Düsseldorf/ Germany
Posts 297
|
|
|
hi ralph,
that means if i use two integers first in a float expression i get an integer result?? mmhhhh
in school i have learnd that 1*360/80 is the same like 360/80*1 with the brackets i would agree if i would use + and -...but its not realy aproblem just good to know
greetz to switzerland
chriss
...........and don't give up :-)
|
|
|
|
|
Report
|
|
|
|
01-21-2008, 13:56
|
framefactory
Joined on 01-11-2006
Zurich, Switzerland
Posts 113
|
|
|
Hi Chriss!
What you learned at school is still valid, but computers do not always calculate according to the fundamental rules of algebra, simply because their accuracy is limited.
You are using a float expression, so all variables you use (A, B, C, D) are floats. Floats can represent rational numbers (Bruchzahlen) in opposite to Integers (Ganzzahlen).
If you write "360" the computer reads this as an Integer. You can also write "360.0f", then the number is interpreted as floating point number (float). Variables in a float expression node however, are always treated as floats. So the variable "A" you are using stands for a value of "1.0f", not "1".
Now comes the important part:
If you write "360/80*A" the computer starts at the left side and first sees two integer numbers and a division operator. Because only integers are involved, it is executed as an integer division. The result is an integer, too: "360/80" = "4". Only then it is multiplied by A, which is "1.0f". As soon as a float number is involved, the computer switches to floating point mode. So the result is "4*1.0f" which evaluates to "4.0f".
Second case: You write "A*360/80". The computer starts from the left side again and first calculates "1.0f*360". A floating point number is involved, so the result is a float: "360.0f". Now the division is calculated: "360.0f/80". Since the left operand is a floating point number, the division is executed in floating point precision! The end result is "4.5f"
Greetings from the swiss math department :-) Ralph
|
|
|
|
|
Report
|
|
|
|
01-21-2008, 23:06
|
chriss0212
Joined on 03-21-2006
Düsseldorf/ Germany
Posts 297
|
|
|
hi ralph,
thats a realy cool expression :-) means: i understand :-) sorry that there is no math thranslation for your explanation but a big thx (hope i will remember next time :-) )
greetz
chriss
...........and don't give up :-)
|
|
|
|
|
Report
|
|
|
|
|
 |