comp.lang.ada
 help / color / mirror / Atom feed
* error output numbers
@ 2001-03-19 20:46 w
  2001-03-19 21:28 ` Marin David Condic
                   ` (3 more replies)
  0 siblings, 4 replies; 6+ messages in thread
From: w @ 2001-03-19 20:46 UTC (permalink / raw)


when i use ada to write a program to calculate factorial, i found that
result after 12! is always wrong.

for example, 

12! = 479001600
but 13! gives me 1932053504.    
The correct result should be 6227020800.

May be there are some problems with my integer declaration?
I decleared all outputs to be INTEGER.

Thanks!       :-)



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

* Re: error output numbers
  2001-03-19 20:46 error output numbers w
@ 2001-03-19 21:28 ` Marin David Condic
  2001-03-20  2:22   ` tmoran
  2001-03-19 21:36 ` David C. Hoos, Sr.
                   ` (2 subsequent siblings)
  3 siblings, 1 reply; 6+ messages in thread
From: Marin David Condic @ 2001-03-19 21:28 UTC (permalink / raw)


The correct result is in excess of six billion. A garden variety integer is
only good to 2**31 - or around two billion - on most Ada implementations.
Hence your calculation is overflowing. You did not say what compiler you are
using but I bet if you check you'll discover there is a switch you can set
to turn on runtime checks for overflows. Do that, and your program will
probably raise Constraint_Error.

If your compiler supports a longer integer, this might help your
calculations, but it is only a matter of time before this will overflow too.
Factorials grow very rapidly to exceed whatever size integer you want to
pick.

MDC
--
Marin David Condic
Senior Software Engineer
Pace Micro Technology Americas    www.pacemicro.com
Enabling the digital revolution
e-Mail:    marin.condic@pacemicro.com
Web:      http://www.mcondic.com/



"w" <wwminirl@hotmail.com> wrote in message
news:01c0b0b5$af29df60$2a9ec594@default...
> when i use ada to write a program to calculate factorial, i found that
> result after 12! is always wrong.
>
> for example,
>
> 12! = 479001600
> but 13! gives me 1932053504.
> The correct result should be 6227020800.
>
> May be there are some problems with my integer declaration?
> I decleared all outputs to be INTEGER.
>
> Thanks!       :-)





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

* Re: error output numbers
  2001-03-19 20:46 error output numbers w
  2001-03-19 21:28 ` Marin David Condic
@ 2001-03-19 21:36 ` David C. Hoos, Sr.
  2001-03-19 22:10 ` Chad R. Meiners
  2001-04-06 14:03 ` Nils
  3 siblings, 0 replies; 6+ messages in thread
From: David C. Hoos, Sr. @ 2001-03-19 21:36 UTC (permalink / raw)


13! is greater than 2**31 -1, so type Integer cannot be used to
hold such a value.

You can use a 64 bit modular type (mod 2 ** 64) to extend the range
somewhat.

"w" <wwminirl@hotmail.com> wrote in message
news:01c0b0b5$af29df60$2a9ec594@default...
> when i use ada to write a program to calculate factorial, i found that
> result after 12! is always wrong.
>
> for example,
>
> 12! = 479001600
> but 13! gives me 1932053504.
> The correct result should be 6227020800.
>
> May be there are some problems with my integer declaration?
> I decleared all outputs to be INTEGER.
>
> Thanks!       :-)





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

* Re: error output numbers
  2001-03-19 20:46 error output numbers w
  2001-03-19 21:28 ` Marin David Condic
  2001-03-19 21:36 ` David C. Hoos, Sr.
@ 2001-03-19 22:10 ` Chad R. Meiners
  2001-04-06 14:03 ` Nils
  3 siblings, 0 replies; 6+ messages in thread
From: Chad R. Meiners @ 2001-03-19 22:10 UTC (permalink / raw)


Perhaps you should compile with the -gnato option ;)
(assuming of course you are using GNAT)

-Chad R. Meiners

"w" <wwminirl@hotmail.com> wrote in message
news:01c0b0b5$af29df60$2a9ec594@default...
> when i use ada to write a program to calculate factorial, i found that
> result after 12! is always wrong.
>
> for example,
>
> 12! = 479001600
> but 13! gives me 1932053504.
> The correct result should be 6227020800.
>
> May be there are some problems with my integer declaration?
> I decleared all outputs to be INTEGER.
>
> Thanks!       :-)





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

* Re: error output numbers
  2001-03-19 21:28 ` Marin David Condic
@ 2001-03-20  2:22   ` tmoran
  0 siblings, 0 replies; 6+ messages in thread
From: tmoran @ 2001-03-20  2:22 UTC (permalink / raw)


> > I decleared all outputs to be INTEGER.
> The correct result is in excess of six billion. A garden variety integer is
> only good to 2**31 - or around two billion - on most Ada implementations.
  This seems an excellent demonstration of the advantages of using your own
declared integer types, rather than whatever your current compiler happens
to be offering today.  If the declaration was
  type Factorial_Values is range 1 .. 999_999_999;
it would be clear that a value of a billion or more won't work.  OTOH,
  type Factorial_Values is range 1 .. 999_999_999_999;
would let the compiler know it better use more than 32 bits, and it
would be clear exactly how big a factorial value would still work.



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

* Re: error output numbers
  2001-03-19 20:46 error output numbers w
                   ` (2 preceding siblings ...)
  2001-03-19 22:10 ` Chad R. Meiners
@ 2001-04-06 14:03 ` Nils
  3 siblings, 0 replies; 6+ messages in thread
From: Nils @ 2001-04-06 14:03 UTC (permalink / raw)


This is 32 bit integer problem, note that
2**32 = 4294967296
+ 1932053504.=
6227020800

try interfaces.integer_64 if you are using gnat
or define your own type

nils

w wrote:

> when i use ada to write a program to calculate factorial, i found that
> result after 12! is always wrong.
>
> for example,
>
> 12! = 479001600
> but 13! gives me 1932053504.
> The correct result should be 6227020800.
>
> May be there are some problems with my integer declaration?
> I decleared all outputs to be INTEGER.
>
> Thanks!       :-)




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

end of thread, other threads:[~2001-04-06 14:03 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2001-03-19 20:46 error output numbers w
2001-03-19 21:28 ` Marin David Condic
2001-03-20  2:22   ` tmoran
2001-03-19 21:36 ` David C. Hoos, Sr.
2001-03-19 22:10 ` Chad R. Meiners
2001-04-06 14:03 ` Nils

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