BASIC User Group > Code Challenges

Simple Question

(1/3) > >>


--- Code: Script BASIC ---PRINT 9999999999999999.0 - 9999999999999998.0,"\n" 
What does your language return?

Script BASIC  2

At 15 decimal places SB returns the correct answer of 1.

--- Code: Bash ---$ perl6 -e 'print 9999999999999999.0-9999999999999998.0;print "\n";'2 
Doesn't work on my Ubuntu 18.10 laptop.

Floating point is 64bit by default, which doesn't offer the required precision for such a large number.

Using a long double, you (based on the compiler implementation) can get ~80bit precision:

--- Code: C++ ---#include <jade.hpp> MAIN     PRINT(9999999999999999.0L - 9999999999999998.0L);END 
$ ./num


--- Code: Text ---$EXECON printf(E"%LG\n", 9999999999999999.0L - 9999999999999998.0L) 
$ ./mbc-num


I tried it again in SB using the FORMAT function and got the following erorr.

--- Code: Script BASIC ---PRINT FORMAT("%LG",9999999999999999.0 - 9999999999999998.0),"\n" 
jrs@jrs-laptop:~/sb/examples/test$ scriba
(0): error &H8:The argument passed to a module function is out of the accepted range.

The format specifier expects 'long double', but the numbers are still 'double'.  So even if it worked without the error, you would still see '2' as the result of the print statement here.

That's what the appended 'L' in my examples does, it tells the compiler that the numbers should be treated as 'long double' instead of the default 'double'...

I think you're out of luck with NATIVE support in SB.  It looks like it only uses Single Precision (REAL)

--- Quote from: SB Docs ---25.202. TYPE

This function can be used to determine the type of an expression. The function returns a numeric value that describes the type of the argument. Although the numeric values are guaranteed to be the one defined here it is recommended that you use the predefined symbolic constant values to compare the return value of the function against. The function return value is the following

    SbTypeUndef 0 if the argument is undef.
    SbTypeString 1 if the argument is string.
    SbTypeReal 2 if the argument is real.
    SbTypeInteger 3 if the argument is integer.
    SbTypeArray 4 if the argument is an array.
--- End quote ---

So if you do:

--- Code: Script BASIC ---PRINT TYPE(9999999999999999.0)
It returns '2', which according to the docs is a REAL (single precision) Number.


[0] Message Index

[#] Next page

Go to full version