Author Topic: Floating point issue  (Read 309 times)

0 Members and 1 Guest are viewing this topic.

Offline Brian Alvarez

  • Full Member
  • ***
  • Posts: 133
    • PluriBASIC
Floating point issue
« 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?:

Code: [Select]
'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:

Code: [Select]
2
-1
130

 I downloaded latest oxygen dll prior to this test.


Offline Brian Alvarez

  • Full Member
  • ***
  • Posts: 133
    • PluriBASIC
Re: Floating point issue
« Reply #1 on: November 13, 2018, 10:11:57 AM »
This is intersting, if i do this:

Code: [Select]
print str(2.1)
It displays: "2"

But if i do this:

Code: [Select]
print "test 1: " &  str(2.1)
It displays this: "test 1: 2.1"


Offline Brian Alvarez

  • Full Member
  • ***
  • Posts: 133
    • PluriBASIC
Re: Floating point issue
« Reply #2 on: November 13, 2018, 10:13:40 AM »
So... doing this:

Code: [Select]
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.
« Last Edit: November 13, 2018, 10:15:41 AM by Brian Alvarez »

Offline Charles Pegge

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 665
    • Charles Pegge
Re: Floating point issue
« Reply #3 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

Code: [Select]
  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
  '


Offline Brian Alvarez

  • Full Member
  • ***
  • Posts: 133
    • PluriBASIC
Re: Floating point issue
« Reply #4 on: November 13, 2018, 12:44:04 PM »

Charles, initial tests are successful! I will keep testing. :)

Offline Brian Alvarez

  • Full Member
  • ***
  • Posts: 133
    • PluriBASIC
Re: Floating point issue
« Reply #5 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()?

Offline Charles Pegge

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 665
    • Charles Pegge
Re: Floating point issue
« Reply #6 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.

Code: [Select]
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"

Offline Brian Alvarez

  • Full Member
  • ***
  • Posts: 133
    • PluriBASIC
Re: Floating point issue
« Reply #7 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? :)

Offline Charles Pegge

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 665
    • Charles Pegge
Re: Floating point issue
« Reply #8 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?

Offline Brian Alvarez

  • Full Member
  • ***
  • Posts: 133
    • PluriBASIC
Re: Floating point issue
« Reply #9 on: November 13, 2018, 10:30:35 PM »
I haven't used it. Does it work like the PowerBASIC formatter?

Offline Charles Pegge

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 665
    • Charles Pegge
Re: Floating point issue
« Reply #10 on: November 14, 2018, 01:57:56 AM »

Offline Karen Zibowski

  • Newbie
  • *
  • Posts: 9
Re: Floating point issue
« Reply #11 on: November 14, 2018, 02:54:46 PM »
Hi Charles,

Quote
Would 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

Offline Brian Alvarez

  • Full Member
  • ***
  • Posts: 133
    • PluriBASIC
Re: Floating point issue
« Reply #12 on: November 14, 2018, 11:48:48 PM »
Yes, please put an example together, seems like a feasible solution. :)

Offline Charles Pegge

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 665
    • Charles Pegge
Re: Floating point issue
« Reply #13 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:

Code: [Select]
'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
« Last Edit: November 15, 2018, 09:19:55 AM by Charles Pegge »

Offline Mike Lobanovsky

  • Jr. Member
  • **
  • Posts: 94
Re: Floating point issue
« Reply #14 on: November 16, 2018, 03:45:57 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?
Mike
(3.6GHz Intel Core i5 w/ 16GB RAM, 2 x GTX 650Ti w/ 2GB VRAM, Windows 7 Ultimate Sp1)