[Scilab-users] How to print int64 or uint64 integers at full accuracy?

Tim Wescott tim at wescottdesign.com
Mon Aug 29 02:09:21 CEST 2016


A kludge would be to make it into two integers.  This works for positive
integers, but verifying (or modifying) the algorithm to signed numbers
is left as an exercise to the reader:

A = (some 64-bit constant)
A_1 = A / 1000000000;
A_0 = A - A_1 * 1000000000;

if (A_1 == 0)
  mprintf("A = %d\n", A);
else
  mprintf("A = %d%09d\n", A_1, A_0);
end // if

Woo hoo!  Eight lines and an if statement where you wanted something
embedded in mprintf.  What could be better?

On Mon, 2016-08-29 at 00:05 +0200, Samuel Gougeon wrote:
> Hello all,
> 
> I am wondering how it is possible to print (in a file, in a string)
> new 64 bit integers,
> at full accuracy. Their relative accuracy is better than for decimal
> numbers
> (1/2^63 instead of %eps=1/2^52).
> 
> In Scilab 5, digits lower than 1/%eps -- that are somewhat randomly
> set --
> can be displayed but are not relevant:
> -->format(24)
> -->%pi
>  %pi  =
>     3.141592653589793115998
> 
> -->(%pi-3.141592653589793)==0
>  ans  =
>   T  
> 
> In Scilab 6, digits beyond %eps are displayed as 0. This is great:
> --> format(24)
> --> %pi
>  %pi  = 
>    3.141592653589793100000
> 
> However, this change is applied to all numbers, even to integers.
> The issue is that Scilab 6 also brings super-integers :) int64 /
> uint64
> having 64-bit mantissae, instead of 53-bit mantissae for decimal
> numbers.
> Hence, int64 have a relative accuracy better than %eps.
> Unfortunately the related digits can't be printed.
> Only the default display in console works.
> Examples:
> i = int64(2)^62 + 1
> printf("%d  %i  %ld  %li \n", i, i ,i, i)
> format(22)
> string(i)
> // I found this undocumented trick with printf format, but there is
> the same truncature:
> printf("%20.0f\n", i)
> 
> // Results:
> --> i = int64(2)^62 + 1
>  i  = 
>   4611686018427387905        // The right value!
> 
> --> printf("%d  %i  %ld  %li \n", i, i ,i, i)
> -2147483648  -2147483648  -2147483648  -2147483648    // Oups. %lld
> does nor work
> 
> --> format(22)
> --> string(i)
>  ans  =
>  4611686018427387900        // bad trailing zeros
> 
> --> // I found this undocumented trick with printf format, but there
> is the same truncature:
> --> printf("%20.0f\n", i)
>  4611686018427387900        // Same issue (final 5 missing)
> 
> 
> I started looking for a solution with write() and its fortran
> formating,
> but it looks no more ready for int64 integers.
> 
> Would you know any solution?
> 
> Thanks
> Samuel
> 
> _______________________________________________
> users mailing list
> users at lists.scilab.org
> http://lists.scilab.org/mailman/listinfo/users

-- 

Tim Wescott
www.wescottdesign.com
Control & Communications systems, circuit & software design.
Phone: 503.631.7815
Cell:  503.349.8432





More information about the users mailing list