LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

großer Rechenfehler trotz float variable

Ich habe folgendes Problem:

 

float amplitude;

 

--------------------------------------------------

amplitude=4000/60*8;

 

Ergebnis: großer Fehler

amplitude=528

--------------------------------------------------

amplitude=8*4000/60;

 

Ergebnis: kleiner Fehler

amplitude=533

--------------------------------------------------

amplitude=4000.0/60*8;

 

Ergebnis: Ok

amplitude=533.333

-------------------------------------------------

Wieso rundet der Compiler so?

Kann der Compiler umgestellt werden?

Machen daß alle Compiler so, oder nur National?

 

Danke für Deine Antwort!

 

 

0 Kudos
Message 1 of 2
(3,713 Views)

Sorry for answering in English Smiley Wink

 

How values are treated by C is greatly influenced on how they are written and the implicit data type associated.

 

4000/60*8:   in this case all values are treated as integers, and the result of the operation is correct in this respect. The large error strictly depend on the (integer) division performed as the first

 

8*4000/60:  similar discussion. The error is smaller since the division is executed last on greater values.

 

4000.0/60*8:    in this case, 4000.0 is considered a double value, and the compiler automatically promotes all other values to the double one, producing correct results. The same would apply if you have a double variable in the calculation: no matter if other variables are integers: they are promoted to double while performing the calculation.

 

In this page automatic type promotion rules are listed.



Proud to use LW/CVI from 3.1 on.

My contributions to the Developer Community
________________________________________
If I have helped you, why not giving me a kudos?
Message 2 of 2
(3,710 Views)