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

Samuel Gougeon sgougeon at free.fr
Mon Aug 29 02:46:27 CEST 2016


In the SCI/modules/string/sci_gateway/cpp/sci_string.cpp source file,
is defined
types::Function::ReturnValue *intString*(T* pInt, types::typed_list &out)
{
     int iDims = pInt->getDims();
     int* piDimsArray = pInt->getDimsArray();
     types::String *pstOutput = new types::String(iDims, piDimsArray);
     int iSize = pInt->getSize();
     for (int i = 0 ; i < iSize ; i++)
     {
         std::wostringstream ostr;
         DoubleComplexMatrix2String(&ostr, *(double)*pInt->get(i), 0);
         pstOutput->set(i, ostr.str().c_str());
     }
where integers are converted into decimal before being sent to 
DoubleComplexMatrix2String()
to be formated as a decimal.
There are at least 3 different algos to do the same thing (i guess not 
only for integers)...:
  - the disp() one (that is OK for integers)
  - the string() one (NOK)
  - the printf() one (NOK)
  - and may be other ones...


Le 29/08/2016 02:09, Tim Wescott a écrit :
> 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

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.scilab.org/pipermail/users/attachments/20160829/62ec7fc2/attachment.htm>


More information about the users mailing list