comp.lang.ada
 help / color / mirror / Atom feed
* evaluation of named numbers
@ 2010-10-17 15:01 Rolf
  2010-10-17 15:28 ` Vinzent Hoefler
                   ` (4 more replies)
  0 siblings, 5 replies; 7+ messages in thread
From: Rolf @ 2010-10-17 15:01 UTC (permalink / raw)


I always thought that named numbers are fully evaluated with (almost)
unlimited precision at compile time. I was quite astonished to see

  Ticks : constant := 20 / 1000 * 16_000_000 / 8; -- 40_000 ticks

evaluated to 0 and not to the expected 40.000. It seems that the
compiler uses integer numbers also for intermediate results and 20 /
1000 ends up to be 0 instead of 0.02. Any decent pocket calculator
gets that right.

The workaround is simple once I understood the problem:

   Ticks : constant := 20 * 16_000_000 / 8 / 1000; --  40_000 ticks

Are expressions for named numbers always evaluated left to right? I
think I'll add an item to the review check list to always put
multiplications on the left before any divisions.

   Rolf



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

end of thread, other threads:[~2010-10-17 22:32 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2010-10-17 15:01 evaluation of named numbers Rolf
2010-10-17 15:28 ` Vinzent Hoefler
2010-10-17 16:05 ` Robert A Duff
2010-10-17 18:32   ` Georg Bauhaus
2010-10-17 18:57 ` Nasser M. Abbasi
2010-10-17 22:30 ` anon
2010-10-17 22:32 ` anon

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