[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [sc-users] Problems with "real" numbers




Am 30.12.2015 um 20:29 schrieb TomD <tomdiscus@xxxxxx>:

So I found a way to use 0.56 instead of its long version, that is to convert it to a string and re-convert it to a float this way :
(x.asString.asFloat == 0.56) -> true

My question is simply : how would any of you do to get the same result (true) if not using asString.asFloat, without using "round" or "trunc" etc. which are uncertain methods ? How do you manage with this kind of problem ?

Cheers (and best wishes to all !)

T



x = 0.1 + 0.1 + 0.1

x == 0.3

-> false


A general programming problem ! 
Equality checks with floats 
have to be done with a defined threshold, e.g.

(x - 0.3).abs < 1e-4    // 1e-4 = 1 * (10 ** -4) = 0.0001

-> true


or use the dedicated method equalWithPrecision,
here with default threshold arg precision = 1e-4

x.equalWithPrecision(0.3)

-> true


Greetings

Daniel


-----------------------------
www.daniel-mayer.at
-----------------------------