Bookstore

 Computer Math and Games in Pascal (preview) Lazarus Handbook

Author Topic: Bug in Math functions Sqrt and Double in LAMW  (Read 1307 times)

neuro

• New Member
• Posts: 26
Bug in Math functions Sqrt and Double in LAMW
« on: May 31, 2021, 03:39:21 pm »
How to fix bug in Math functions Sqrt and Double in LAMW?

Code: Pascal  [Select][+][-]
1. jTextView1.Text := FloatToStr(Sqrt(36)); // <<-- produces correct answer
2. jTextView2.Text := FloatToStr(Sqrt(Double(36))); // <<-- produces wrong answer, produces very small Double number which is very close to zero.

Both lines should produce the same result.
In Windows, Linux, macOS it produces correct answer. In Android it fails.
How to fix this bug?
« Last Edit: May 31, 2021, 04:26:57 pm by neuro »

wp

• Hero Member
• Posts: 8573
Re: Bug in Math functions Sqrt and Double in LAMW
« Reply #1 on: May 31, 2021, 06:11:12 pm »
Code: Pascal  [Select][+][-]
1. Double(36)
I would not use this code at all because I never know whether this converts the integer 36 to a double value, or casts the bit presentation of the integer to the bit presentation of a double. Why don't you simply type the old-fashioned "36.0"?
Code: Pascal  [Select][+][-]
1. jTextView2.Text := FloatToStr(Sqrt(36.0));
« Last Edit: May 31, 2021, 07:04:02 pm by wp »
Mainly Lazarus trunk / fpc 3.2.0 / all 32-bit on Win-10, but many more...

Alextp

• Hero Member
• Posts: 1331
Re: Bug in Math functions Sqrt and Double in LAMW
« Reply #2 on: May 31, 2021, 06:39:31 pm »
Quote
I never know whether this converts the integer 36 to a double value
Person said that it produced the correct result on several OS'es.

MarkMLl

• Hero Member
• Posts: 2869
Re: Bug in Math functions Sqrt and Double in LAMW
« Reply #3 on: May 31, 2021, 06:47:42 pm »
Quote
I never know whether this converts the integer 36 to a double value
Person said that it produced the correct result on several OS'es.

Without specifying precisely which OSes, their wordsize or endianness, or showing the results.

It's not beyond the bounds of possibility for Sqrt(36.0) to be computed as 8.00000001

MarkMLl
Turbo Pascal v1 on CCP/M-86, multitasking with LAN and graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

• Hero Member
• Posts: 10795
Re: Bug in Math functions Sqrt and Double in LAMW
« Reply #4 on: May 31, 2021, 06:57:56 pm »
Correct, but I would add that hardcast should be avoived. I suspect that contributes to the issue.

engkin

• Hero Member
• Posts: 2861
Re: Bug in Math functions Sqrt and Double in LAMW
« Reply #5 on: May 31, 2021, 07:56:48 pm »
It is a bug. Similar to this one.