Why is this result not zero?
set x -9408.8
puts [expr {$x*$x + $x*$x + $x*$x - $x*$x - $x*$x - $x*$x}]
This is a simplified demo of a more complex equation:
set x -9408.8
set y -9408.8
set z -9408.8
puts [expr {($x*$x + $y*$y + $z*$z - $x*$y - $y*$z - $z*$x)}]
Why is this result not zero?
set x -9408.8
puts [expr {$x*$x + $x*$x + $x*$x - $x*$x - $x*$x - $x*$x}]
This is a simplified demo of a more complex equation:
set x -9408.8
set y -9408.8
set z -9408.8
puts [expr {($x*$x + $y*$y + $z*$z - $x*$y - $y*$z - $z*$x)}]
Why is this result not zero?
set x -9408.8
puts [expr {$x*$x + $x*$x + $x*$x - $x*$x - $x*$x - $x*$x}]
This is a simplified demo of a more complex equation:
set x -9408.8
set y -9408.8
set z -9408.8
puts [expr {($x*$x + $y*$y + $z*$z - $x*$y - $y*$z - $z*$x)}]
Alexandru <alexandr...@meshparts.de> wrote:
Why is this result not zero?
set x -9408.8
puts [expr {$x*$x + $x*$x + $x*$x - $x*$x - $x*$x - $x*$x}]
This is a simplified demo of a more complex equation:
set x -9408.8See https://floating-point-gui.de/ (and read most of the material
set y -9408.8
set z -9408.8
puts [expr {($x*$x + $y*$y + $z*$z - $x*$y - $y*$z - $z*$x)}]
there).
What you are seeing is /normal/ for binary floating point math.
Why is this result not zero?
set x -9408.8
puts [expr {$x*$x + $x*$x + $x*$x - $x*$x - $x*$x - $x*$x}]
Rich schrieb am Dienstag, 28. Juni 2022 um 23:14:28 UTC+2:
Alexandru <alexandr...@meshparts.de> wrote:
Why is this result not zero?
set x -9408.8
puts [expr {$x*$x + $x*$x + $x*$x - $x*$x - $x*$x - $x*$x}]
This is a simplified demo of a more complex equation:
set x -9408.8See https://floating-point-gui.de/ (and read most of the material
set y -9408.8
set z -9408.8
puts [expr {($x*$x + $y*$y + $z*$z - $x*$y - $y*$z - $z*$x)}]
there).
What you are seeing is /normal/ for binary floating point math.
Thank you all.
I actually aware of those numerical issues.
But 1e-8 suprized me alot, because it's actually a large number in my opinion.
I wonder, if same operation in C would give better results.
Another intersting thing, changing order of terms, leads to clean zero:
expr $x*$x - $x*$y + $y*$y - $y*$z + $z*$z - $z*$x
is 0.0
Floating point math 101: NEVER, EVERY do an == compare of a floating point computation to 0.0 as part of a loop condition. If you do this the
loop may never stop. ALWAYS do a < compare to some very small number (a "fuzz"
factor).
Rich schrieb am Dienstag, 28. Juni 2022 um 23:14:28 UTC+2:
Alexandru <alexandr...@meshparts.de> wrote:
Why is this result not zero?See https://floating-point-gui.de/ (and read most of the material
set x -9408.8
puts [expr {$x*$x + $x*$x + $x*$x - $x*$x - $x*$x - $x*$x}]
This is a simplified demo of a more complex equation:
set x -9408.8
set y -9408.8
set z -9408.8
puts [expr {($x*$x + $y*$y + $z*$z - $x*$y - $y*$z - $z*$x)}]
there).
What you are seeing is /normal/ for binary floating point math.
Thank you all.
I actually aware of those numerical issues.
But 1e-8 suprized me alot, because it's actually a large number in my opinion.
I wonder, if same operation in C would give better results.
Another intersting thing, changing order of terms, leads to clean zero:
package require MpexprNice package. I'm afraid the performance will worsen much, as the computation is done many times and speed is important.
set x -9408.8
puts [mpexpr {$x*$x + $x*$x + $x*$x - $x*$x - $x*$x - $x*$x}]
# => 0.0
set x -9408.8
set y -9408.8
set z -9408.8
puts [mpexpr {($x*$x + $y*$y + $z*$z - $x*$y - $y*$z - $z*$x)}]
# => 0.0
Yusuke Yamasaki schrieb am Mittwoch, 29. Juni 2022 um 09:18:23 UTC+2:
package require Mpexpr
set x -9408.8Nice package. I'm afraid the performance will worsen much, as the computation is done many times and speed is important.
puts [mpexpr {$x*$x + $x*$x + $x*$x - $x*$x - $x*$x - $x*$x}]
# => 0.0
set x -9408.8
set y -9408.8
set z -9408.8
puts [mpexpr {($x*$x + $y*$y + $z*$z - $x*$y - $y*$z - $z*$x)}]
# => 0.0
Rich schrieb am Dienstag, 28. Juni 2022 um 23:14:28 UTC+2:
Alexandru <alexandr...@meshparts.de> wrote:
Why is this result not zero?
set x -9408.8
puts [expr {$x*$x + $x*$x + $x*$x - $x*$x - $x*$x - $x*$x}]
and this makes sense because each term has the same 64-bit representation.See https://floating-point-gui.de/ (and read most of the material
This is a simplified demo of a more complex equation:
set x -9408.8
set y -9408.8
set z -9408.8
puts [expr {($x*$x + $y*$y + $z*$z - $x*$y - $y*$z - $z*$x)}]
there).
What you are seeing is /normal/ for binary floating point math.
Thank you all.
I actually aware of those numerical issues.
But 1e-8 suprized me alot, because it's actually a large number in my opinion.
I wonder, if same operation in C would give better results.
Another intersting thing, changing order of terms, leads to clean zero:
expr $x*$x - $x*$y + $y*$y - $y*$z + $z*$z - $z*$x
is 0.0
Sysop: | Keyop |
---|---|
Location: | Huddersfield, West Yorkshire, UK |
Users: | 307 |
Nodes: | 16 (2 / 14) |
Uptime: | 56:27:14 |
Calls: | 6,914 |
Calls today: | 4 |
Files: | 12,379 |
Messages: | 5,430,690 |