Mailing list for all users of the OCaml language and system.
 help / color / mirror / Atom feed
* if (n:int) < 0 then (-n) > 0 is FALSE
@ 2006-12-07 18:32 Pal-Kristian Engstad
  2006-12-07 19:19 ` [Caml-list] " Jonathan Roewen
  2006-12-07 21:13 ` Martin Jambon
  0 siblings, 2 replies; 11+ messages in thread
From: Pal-Kristian Engstad @ 2006-12-07 18:32 UTC (permalink / raw)
  To: caml-list

Using OCaml 3.08.3 on the following function:

let pow x n =
  let rec aux x n acc =
    if n == 0 then acc
    else if n == 1 then acc *. x
    else if n land 1 == 0 then aux (x*.x) (n/2) acc
    else aux (x*.x) ((n-1)/2) (x*.acc)
  in
    if n >= 0 then
      aux x n 1.0
    else
      1.0 /. (aux x (-n) 1.0)
;;

I tested this function with

# pow 1.0 (1024 * 1024 * 1024)

To find that it loops forever. The reason is that 1024*1024*1024=2^30 
cannot be represented as a positive number on 32-bit platforms, hence it 
silently converts it to -1073741824, or -2^30. The reason this loops 
again is that -n = -(-20^30) = -20^30......, still negative!

This is obviously a bug - has it since been fixed? But more alarmingly - 
why is there no warning?

Thanks,

PKE.

-- 
Pål-Kristian Engstad (engstad@naughtydog.com), Lead Programmer, ICE
team, Naughty Dog, Inc., 1601 Cloverfield Blvd, 6000 North,
Santa Monica, CA 90404, USA. Ph.: (310) 633-9112.

"Most of us would do well to remember that there is a reason Carmack
is Carmack, and we are not Carmack.",
                       Jonathan Blow, 2/1/2006, GD Algo Mailing List




^ permalink raw reply	[flat|nested] 11+ messages in thread

end of thread, other threads:[~2006-12-09  2:03 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2006-12-07 18:32 if (n:int) < 0 then (-n) > 0 is FALSE Pal-Kristian Engstad
2006-12-07 19:19 ` [Caml-list] " Jonathan Roewen
2006-12-07 19:43   ` Mattias Engdegård
2006-12-07 20:02     ` Brian Hurt
2006-12-08  2:37     ` skaller
2006-12-08 11:09       ` Mattias Engdegård
2006-12-08 17:48     ` Jon Harrop
2006-12-08 18:20       ` Mattias Engdegård
2006-12-08 18:37         ` Brian Hurt
2006-12-09  2:03           ` skaller
2006-12-07 21:13 ` Martin Jambon

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox