Alaska Software Inc. - Round function
Username: Password:
AuthorTopic: Round function
Andrej ObermajerRound function
on Mon, 04 Nov 2013 10:24:46 +0100
Hi.


Why Round(364.9 * 0.15 ,2) gives me result 54.73 and not 54.74 ?

Alaska 32-Bit Linker Version 1.90.355


BR
Andrej Obermajer
Thomas BraunRe: Round function
on Mon, 04 Nov 2013 14:15:00 +0100
Andrej Obermajer wrote:

> Why Round(364.9 * 0.15 ,2) gives me result 54.73 and not 54.74 ?

When I do this:

   ? (364.9 * 0.15)
   ? Round(364.9 * 0.15, 2)

I get this (expected) results

   54.735
   54.74

So most likely one or both of the two operands in your example above is the
result of another arithmetic operation?

Probably you are hit by the fact that the IEEE 754 floating point number
representation scheme used by many programming languages is not able to
represent any possible floating point number with 100% accuracy (hint:
there are an infinite number of them 

Even a simple number like 0.1 ist not representable in a binary notaiton
with  a finite nunmber of bits.

http://en.wikipedia.org/wiki/Floating_point#Accuracy_problems

To solve this, round results of arithmetic operations to the needed
accuracy.

Thomas
Hubert BrandelRe: Round function
on Sun, 10 Nov 2013 18:30:44 +0100
Am 04.11.2013 10:24, schrieb Andrej Obermajer:
> Why Round(364.9 * 0.15 ,2) gives me result 54.73 and not 54.74 ?
> Alaska 32-Bit Linker Version 1.90.355

Hi,

same version ...

FUNCTION MAIN ()
    local nD
    nD = 364.9
    ? nD
    nD *= 0.15
    ? nD, ROUND(nD,2),STR(nD,10,2)
    inkey(10)
return 0

RESULT

        364,9
         54,735         54,74      54.74

so no error !