digitalmars.D.learn - Floating-Point arithmetic in dlang - Difference to other languages
- Jan =?UTF-8?B?SMO2bmln?= (8/8) Dec 03 2019 Today i have stumbled on Hacker News into:
- mipri (13/21) Dec 03 2019 You can get this result in D as well:
- Ola Fosheim =?UTF-8?B?R3LDuHN0YWQ=?= (4/6) Dec 03 2019 Not only historical, it is also for numerical reasons. You can
- Ola Fosheim =?UTF-8?B?R3LDuHN0YWQ=?= (7/9) Dec 03 2019 You get the same in C++ with:
- Basile B. (2/10) Dec 03 2019 D lang could have a very good sprintf replacement.
- H. S. Teoh (13/25) Dec 03 2019 In short, because they use 64-bit floats for intermediate values
Today i have stumbled on Hacker News into: https://0.30000000000000004.com/ I am learning D, that's why i have to ask. Why does writefln("%.17f", .1+.2); not evaluate into: 0.30000000000000004, like C++ but rather to: 0.29999999999999999 Many other languages evaluate to 0.30000000000000004 as well.
Dec 03 2019
On Tuesday, 3 December 2019 at 09:22:49 UTC, Jan Hönig wrote:Today i have stumbled on Hacker News into: https://0.30000000000000004.com/ I am learning D, that's why i have to ask. Why does writefln("%.17f", .1+.2); not evaluate into: 0.30000000000000004, like C++ but rather to: 0.29999999999999999 Many other languages evaluate to 0.30000000000000004 as well.You can get this result in D as well: $ rdmd --eval 'double a = .1, b = .2; writefln("%.17f", a+b)' 0.30000000000000004 https://dlang.org/spec/float.html explains it: real (80bit, 'long double' in C) floats are used in your first calculation, and doubles are used in my revised example. Most other languages give you the double result for very reasonable historical reasons, described here: https://retrocomputing.stackexchange.com/questions/9751/did-any-compiler-fully-use-intel-x87-80-bit-floating-point/9760#9760 D's behavior is a minor 'miss': https://nwcpp.org/Oct-2019.html ... I wanted to include a C example that gives the D result, but it seems to be trickier to force 80 bit intermediates.
Dec 03 2019
On Tuesday, 3 December 2019 at 09:52:18 UTC, mipri wrote:Most other languages give you the double result for very reasonable historical reasonsNot only historical, it is also for numerical reasons. You can get very unpredictable results if you do compares and compiletime evalution is different from runtime evaluation.
Dec 03 2019
On Tuesday, 3 December 2019 at 09:22:49 UTC, Jan Hönig wrote:not evaluate into: 0.30000000000000004, like C++ but rather to: 0.29999999999999999You get the same in C++ with: #include <cstdio> int main() { printf("%.17f",double(0.1L + 0.2L)); }
Dec 03 2019
On Tuesday, 3 December 2019 at 09:22:49 UTC, Jan Hönig wrote:Today i have stumbled on Hacker News into: https://0.30000000000000004.com/ I am learning D, that's why i have to ask. Why does writefln("%.17f", .1+.2); not evaluate into: 0.30000000000000004, like C++ but rather to: 0.29999999999999999 Many other languages evaluate to 0.30000000000000004 as well.D lang could have a very good sprintf replacement.
Dec 03 2019