Powerbasic Museum 2020-B

IT-Consultant: Charles Pegge => OxygenBasic => Topic started by: Brian Alvarez on November 13, 2018, 09:54:56 AM

Title: Floating point issue
Post by: Brian Alvarez on November 13, 2018, 09:54:56 AM
 
I Finally have some time to work on fixing that was stpping me like one month ago.
Can somebody compile this?:

'Generated with PluriBASIC 6.0.74371.0

$ filename "hello_world.exe"

uses rtl64



' STARTS STR$.BIN

' Enter the stock code and functions here.


' END OF STR$.BIN
' STARTS PLURIBASIC_INIT.BIN

' Enter the stock code and functions here.

' END OF PLURIBASIC_INIT.BIN
' STARTS CALLBACKDATA.BIN

' CALLBACK data

' END OF CALLBACKDATA.BIN
' STARTS ENTRY_POINT.BIN

' Enttry point code
' END OF ENTRY_POINT.BIN

' Initializes various things in the script.
FUNCTION PluriBASIC_Initialize() AS LONG

END FUNCTION


finit
function formt(double d, string f="") as string
return str(d)
end function

FUNCTION PBMAIN() AS LONG
   DOUBLE d
   STRING s
   d = 1.6
   s = STR(d)
   
           print formt(1.4+1.1)
           short b= -1
           print formt(b)
           string b=130.5
           print formt(b)

END FUNCTION

PBMAIN() ' invoke entry point


I am getting:

2
-1
130


I downloaded latest oxygen dll prior to this test.

Title: Re: Floating point issue
Post by: Brian Alvarez on November 13, 2018, 10:11:57 AM
This is intersting, if i do this:

print str(2.1)

It displays: "2"

But if i do this:

print "test 1: " &  str(2.1)

It displays this: "test 1: 2.1"

Title: Re: Floating point issue
Post by: Brian Alvarez on November 13, 2018, 10:13:40 AM
So... doing this:

return ltrim(" " & str(d))

Solved the decimal issue... but i would like to see a fix better than that, one that preserves the space that STR should return for positive numbers.
Title: Re: Floating point issue
Post by: Charles Pegge on November 13, 2018, 11:53:17 AM
Hi Brian,

I've attached the final FB-compiled o2 below. Let's see if it resolves the rounding problem.

To change the number format, so that positive numbers have a leading space, include this line below rtl64:

num.sns=1


  type numformat
    int dp   ' DECIMAL PLACES
    int trz  ' STRIP TRAILING ZEROS
    int sn   ' SCIENTIFIC NOTATION BY DEFAULT
    int sdp  ' INHIBIT ZERO BEFORE DECIMAL POINT
    int sns  ' LEADING SPACE FOR NON NEGATIVE NUMBERS
    int lps  ' LEAD PADDING SPACES
  end type

  'default settins in the RTLs:
'
  '---------------------
  'NUMBER FORMAT CONTROL
  '=====================
  '
  num.dp =16 ' DECIMAL PLACES
  num.trz= 1 ' STRIP TRAILING ZEROS
  num.sn = 0 ' SCIENTIFIC NOTATION BY DEFAULT
  num.sdp= 0 ' INHIBIT ZERO BEFORE DECIMAL POINT


  'implementation in RTL float_to_ascii:
  '
  'PADDING FOR NON-NEGATIVE NUMBERS
  '
  cmp num.sns,0
  jz fwd nex
  mov byte ptr [rdx],32
  inc rdx
  '


Title: Re: Floating point issue
Post by: Brian Alvarez on November 13, 2018, 12:44:04 PM

Charles, initial tests are successful! I will keep testing. :)
Title: Re: Floating point issue
Post by: Brian Alvarez on November 13, 2018, 12:45:33 PM
Is there a way to override o2 functions?.. i mean, is there a way to create a function STR()?
Title: Re: Floating point issue
Post by: Charles Pegge on November 13, 2018, 02:18:18 PM
Yes, you can override str completely. The auto-converter does not use str, so you won't get recursion problems.


function str(double d, string fmt="") as string
===============================================
'return d
return fmt+d
end function

'print " " 1.23
'print str 1.23
'print str 1.23,"XYZ"
Title: Re: Floating point issue
Post by: Brian Alvarez on November 13, 2018, 05:43:26 PM
 I would still like to call the real str from the overrider str...

Yes... its a mess isnt it? :)
Title: Re: Floating point issue
Post by: Charles Pegge on November 13, 2018, 07:17:37 PM
You can create your own str, and still use the original, if your str has a different prototype, without default params. So the compiler can choose which polymorph to use.


Would you be interested in using msvcrt (ms c run-time) for formatted strings?
Title: Re: Floating point issue
Post by: Brian Alvarez on November 13, 2018, 10:30:35 PM
I haven't used it. Does it work like the PowerBASIC formatter?
Title: Re: Floating point issue
Post by: Charles Pegge on November 14, 2018, 01:57:56 AM
Quite different:

'powerbasic format
'http://www.manmrk.net/tutorials/basic/PowerBASIC/PBWINH/FORMAT_function.html

'c printf / sprintf
'http://www.cplusplus.com/reference/cstdio/printf/
Title: Re: Floating point issue
Post by: Karen Zibowski on November 14, 2018, 02:54:46 PM
Hi Charles,

QuoteWould you be interested in using msvcrt (ms c run-time) for formatted strings?

Yes, please teach us how to implement msvcrt formatting in OxygenBasic
perhaps with some examples.
Thank you
Title: Re: Floating point issue
Post by: Brian Alvarez on November 14, 2018, 11:48:48 PM
Yes, please put an example together, seems like a feasible solution. :)
Title: Re: Floating point issue
Post by: Charles Pegge on November 15, 2018, 09:17:59 AM
mscvrt.inc is included in corewin

sprintf requires a char buffer, a formatting string, and 0 or more variadic data.

http://www.cplusplus.com/reference/cstdio/printf/

This is a very simple format example:


'msvcrt val/str/format$ equivalent
'nb: msvcrt downgrades extended precision to double.

  uses msvcrt 'or corewin
  char odata[64]
  sprintf(odata,"%g", double 1/3)
  print odata '0.333333
  sprintf(odata,"%lli", quad 9/5)
  print odata '2
Title: Re: Floating point issue
Post by: Mike Lobanovsky on November 16, 2018, 03:45:57 AM
Quote from: Charles Pegge on November 15, 2018, 09:17:59 AM
...............

  sprintf(odata,"%lli", quad 9/5)

...............

Charles,

IIRC %lli is an originally Linuxoid formatter not recognized by MS Windows native msvcrt.dll's formatted IO functions. You are supposed to use %I64 instead. %lli may be used in later MS VC++ builds but the latter links against MS VC++ -specific libraries rather than msvcrt.dll.

If nonetheless you can still use %lli in OxygenBasic, it means you're substituting it with its Windows-legit counterpart somewhere in the O2 internals. In other words, IMHO you're making O2 non-conformant with your own hands... Or am I missing something?
Title: Re: Floating point issue
Post by: Charles Pegge on November 16, 2018, 04:19:19 AM
Thanks, Mike.

I wonder what other variations there are in the C99 / C++ standard.