Hacker News new | past | comments | ask | show | jobs | submit login

  This is the TXR Lisp interactive listener of TXR 214.
  Quit with :quit or Ctrl-D on empty line. Ctrl-X ? for cheatsheet.
  1> (defun mean (seq) (/ (sum seq) (len seq)))
  mean
  2> (mean '(8.988465674311579e+307 8.98846567431158e+307))
  ** out-of-range floating-point result
  ** during evaluation at expr-1:1 of form (sum seq)
Works for me; error is caught. Breaking news: floating-point has a limited range! Detailed story at 11 ...

Almost any non-trivial calculation that overflows can be massaged into something that avoids overflow. One important motivation behind floating point is to have enough practical range so that you don't run into overflow.

If this is an actual practical problem someone faces, the tool to reach for is wider floating-point, not to mess with the code. A bigger "E" than +307 is a thing.

Also, multi-precision floating-point is a thing.




Or one could cheat, :) by using Scheme exact numbers:

  #lang racket/base
  (define (mean seq) (/ (apply + seq) (length seq)))
  (mean '(#e8.988465674311579e+307 #e8.98846567431158e+307))
89884656743115795000[...]


Similar correct result from sbcl.


The pedantic point of the article is that an error in this situation isn't correct. The average is 8e307 and 8e307 is 8e307.


Agreed. My limited point was that detection of out-of-range is available out of the box in some languages.




Consider applying for YC's Spring batch! Applications are open till Feb 11.

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: