Why (* 1.1 1.1) 1.2100000000000002 in a racket?

When I try the following in a racket:

(* 1.1 1.1) 

why does he return

 1.2100000000000002 

instead

 1.21 

while

 (* 1.2 1.2) ; is 1.44, as expected 

Edit

and the following returns false:

 (= (* 1.1 1.1) 1.21); #f 
+4
source share
1 answer

Look, this compares correctly!

 > (= (* #e1.1 #e1.1) #e1.21) #t 

(Racket has this really nice feature that #e1.1 actually reads as exactly 1.1. Other implementations of the circuit don't necessarily work that way and can actually read in #e1.1 as a floating point and then convert to exact.)

In any case, in order to clarify all the other points, without the precision specifier, Scheme processes the numeric literal with the point in it as inaccurate (that is, 1.1 and #i1.1 , read as the same thing). And with inaccurate numbers, you cannot expect reliable results when making “exact” comparisons (for example, = ).

R7RS Section 6.2.5: “If the written representation of the number does not have the precision pre fi x, the constant is inaccurate if it contains a decimal point or exponent. Otherwise, it is exact.”

+9
source

Source: https://habr.com/ru/post/1498886/


All Articles