Menu

#159 unnecessary and harmful underflow error in vector

open
nobody
None
5
2022-07-05
2022-07-05
No

In bltVecMath.c the test for underflow in vector expr calculations produces unexpected behaviour, an error "floating-point value too small to represent" . If the number is "too small to represent", the calculation should simply return a zero. This is the default tcl behaviour, and when blt vector offers a different set of internal rules, that's disorienting. I came across it when calculating a sum of a few Gaussian lines, exp(-x^2). If I specify the x vector to be too wide, or the standard deviation too small, the rapid decay of the exponential should simply yield zeros far away from the center. Instead, it errors out, and I need to add unnecessary error checking, which completely defeats the elegance of the vector expr calculations of BLT.

package require BLT
namespace import ::blt::*
vector create x
::x seq 25 30 1

puts "\n\ttclsh underflow with y = exp(-x**2)\n"
# tcl [expr ...] returns zeros for x>27
for {set i 0} {$i < [::x length]} {incr i} {
  puts [format "%.2f --> %.3e" $::x($i) [expr exp(-($::x($i)**2))] ]
  }

puts "\n\tblt underflow with y = exp(-x^2)\n"

vector create y
# this works fine
::x seq 25 27 1
puts "$::x(:) -->"
::y expr {exp(-(::x ^ 2))}
puts $::y(:)

# this crashes out with "floating-point value too small to represent"
::x seq 25 28 1
puts "$::x(:) -->"
::y expr {exp(-(::x ^ 2))}
puts $::y(:)

Tested under Ubuntu 20.04, tcl 8.6, blt 2.5.3, but the same code is present in all available versions, including 2.4.7z.

Related

Bugs: #159

Discussion

  • Edward Sternin

    Edward Sternin - 2022-07-05

    Just updated the code from this site, compiled and verified that the same behaviour is seen in the current version (3.0? 4.0?) of BLT:

    $ blt-src/src/bltsh30 fpbug.tcl 
    
        tclsh underflow with y = exp(-x**2)
    
    25.00 --> 3.681e-272
    26.00 --> 2.612e-294
    27.00 --> 2.508e-317
    28.00 --> 0.000e+00
    29.00 --> 0.000e+00
    30.00 --> 0.000e+00
    
        blt underflow with y = exp(-x^2)
    
    25.0 26.0 27.0 -->
    3.6808558548018004e-272 2.6117417612840555e-294 2.507972e-317
    25.0 26.0 27.0 28.0 -->
    floating-point value too small to represent
        while executing
    "::y expr {exp(-(::x ^ 2))}"
        (file "fpbug.tcl" line 25)
    
     
  • George A. Howlett

    I agree that the behavior should be the same as TCL.

    I'll make the changes.

    --gah

    On 7/5/22 13:17, Edward Sternin wrote:


    [bugs:#159] https://sourceforge.net/p/blt/bugs/159/ unnecessary and
    harmful underflow error in vector

    Status: open
    Group:
    Created: Tue Jul 05, 2022 06:17 PM UTC by Edward Sternin
    Last Updated: Tue Jul 05, 2022 06:17 PM UTC
    Owner: nobody

    In |bltVecMath.c| the test for underflow in vector |expr| calculations
    produces unexpected behaviour, an error "floating-point value too
    small to represent" . If the number is "too small to represent", the
    calculation should simply return a zero. This is the default tcl
    behaviour, and when blt vector offers a different set of internal
    rules, that's disorienting. I came across it when calculating a sum of
    a few Gaussian lines, |exp(-x^2)|. If I specify the |x| vector to be
    too wide, or the standard deviation too small, the rapid decay of the
    exponential should simply yield zeros far away from the center.
    Instead, it errors out, and I need to add unnecessary error checking,
    which completely defeats the elegance of the vector |expr|
    calculations of BLT.

    |package require BLT namespace import ::blt:: vector create x ::x seq
    25 30 1 puts "\n\ttclsh underflow with y = exp(-x
    *2)\n" # tcl [expr
    ...]
    returns zeros for x>27 for {set i 0} {$i < [::x length]} {incr i}
    { puts [format "%.2f--> %.3e" $::x($i) [expr exp(-($::x($i)**2))] ] }
    puts "\n\tblt underflow with y = exp(-x^2)\n" vector create y # this
    works fine ::x seq 25 27 1 puts "$::x(:) -->" ::y expr {exp(-(::x ^
    2))} puts $::y(:) # this crashes out with "floating-point value too
    small to represent" ::x seq 25 28 1 puts "$::x(:) -->" ::y expr
    {exp(-(::x ^ 2))} puts $::y(:) |

    Tested under Ubuntu 20.04, tcl 8.6, blt 2.5.3, but the same code is
    present in all available versions, including 2.4.7z.


    Sent from sourceforge.net because you indicated interest in
    https://sourceforge.net/p/blt/bugs/159/

    To unsubscribe from further messages, please visit
    https://sourceforge.net/auth/subscriptions/

     
    👍
    1

    Related

    Bugs: #159


Log in to post a comment.

MongoDB Logo MongoDB