caml-list - the Caml user's mailing list
 help / color / mirror / Atom feed
* [Caml-list] string_of_float (0.1 +. 0.2)
@ 2022-06-15  1:59 Kenichi Asai
  2022-06-15  6:22 ` Andreas Rossberg
  2022-06-15 14:07 ` Gabriel Scherer
  0 siblings, 2 replies; 9+ messages in thread
From: Kenichi Asai @ 2022-06-15  1:59 UTC (permalink / raw)
  To: caml-list

On OCaml 4.12.0 on M1 mac, I got:

# 0.1 +. 0.2;;
- : float = 0.300000000000000044
# string_of_float (0.1 +. 0.2);;
- : string = "0.3"

Why don't I obtain "0.300000000000000044" here?

Here is some background.  I am writing an OCaml interpreter that
mimics most part of the original OCaml interpreter.  In the OCaml
interpreter, 0.1 and 0.2 are represented as

Pexp_constant (Pconst_float ("0.1", None))
Pexp_constant (Pconst_float ("0.2", None))

When I add these two numbers, I would have to execute

let a = float_of_string "0.1"
let b = float_of_string "0.2"
let c = a +. b
let d = string_of_float c

and then return

Pexp_constant (Pconst_float (d, None))

At this point, however, since d is "0.3" instead of
"0.300000000000000044" (even though c is 0.300000000000000044), I
cannot return 0.300000000000000044 as a result.  How can I mimic the
OCaml behavior?

Sincerely,

-- 
Kenichi Asai

^ permalink raw reply	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2022-06-16  9:14 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-06-15  1:59 [Caml-list] string_of_float (0.1 +. 0.2) Kenichi Asai
2022-06-15  6:22 ` Andreas Rossberg
2022-06-15  7:00   ` François Pottier
2022-06-15 14:07 ` Gabriel Scherer
2022-06-15 14:25   ` Daniel Bünzli
2022-06-16  1:45     ` Kenichi Asai
2022-06-16  6:24       ` Oleg
2022-06-16  9:01         ` Andreas Rossberg
2022-06-16  9:14           ` [Caml-list] unsubscribe Jean-Denis EIDEN JEAN-DENIS

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).