0

using tcl 8.6, when run:

% expr 0.1 + 0.1 + 0.1

the expected value is 0.3 but instead get this

0.30000000000000004

could someone tell me how to get the right answer?

robbykz
  • 1
  • 4
  • You'd use [`format`](http://tcl.tk/man/tcl8.6/TclCmd/format.htm) in tcl to get a desired precision. – Shawn Mar 02 '21 at 00:17
  • Oh, and [always brace your expr-essions](https://wiki.tcl-lang.org/page/Brace+your+expr%2Dessions). – Shawn Mar 02 '21 at 00:19
  • @Shawn, I can't see how to get the right answer with tcl in the post you suggest, the code looks like javascipt, maybe is. – robbykz Mar 02 '21 at 00:24
  • It's _exactly_ that issue. Tcl uses normal double precision floats (for computation about floats) so that kind of calculation will be hit by that problem. You can `format %.15g` the number to paper over the issue. Note that Tcl 8.6 is extremely careful to not lose bits of significance when converting to a string, but in this case that's what you want to do. Another option is the [bigfloat](https://tools.ietf.org/doc/tcllib/html/bigfloat.html) package in tcllib. – Donal Fellows Mar 02 '21 at 09:06

0 Answers0