Hi Tyler,
Calling writeln() on an integer always prints the exact value of that integer.
Whereas writing is as a real number may lose precision, as you observe with 20!
Note that 'uint' allocates only 64 bits to store the value, which is not enough
for 21!. Try using 'bigint' instead.
https://chapel-lang.org/docs/modules/standard/BigInteger.html#module-BigInteger
Vass
-----Original Message-----
From: Tyler Simon <[email protected]>
Date: Monday, July 22, 2019 at 5:22 PM
To: Vassily Litvinov <[email protected]>
Cc: "[email protected]"
<[email protected]>
Subject: Re: [Chapel-developers] writeln formatting
Thank You Vass,
I am still not able to get an accurate result above 20! even when converting to
a real. Here is the code with output. I am trying to be as simple as possible,
thanks.
config const value:uint = 10;
var result:uint;
proc factorial(x: uint) : uint
{
if x < 0 then
halt("need a positive number");
return if x == 0 then 1 else x * factorial(x-1);
}
result = factorial(value);
writeln("factorial ", value, "= ",result);
writef("%er ", result:real);
writeln();
using Chapel version 1.18.
output (incorrect):
./factchpl -svalue=21
factorial 21= 14197454024290336768
1.419745e+19
output (correct):
./factchpl -svalue=20
factorial 20= 2432902008176640000
2.432902e+18
Regards,
-Tyler
_______________________________________________
Chapel-developers mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/chapel-developers