• Welcome to Jose's Read Only Forum 2023.
 

Floating point issue

Started by Brian Alvarez, November 13, 2018, 09:54:56 AM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Brian Alvarez

 
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.


Brian Alvarez

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"


Brian Alvarez

#2
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.

Charles Pegge

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
  '



Brian Alvarez


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

Brian Alvarez

Is there a way to override o2 functions?.. i mean, is there a way to create a function STR()?

Charles Pegge

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"

Brian Alvarez

 I would still like to call the real str from the overrider str...

Yes... its a mess isnt it? :)

Charles Pegge

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?

Brian Alvarez

I haven't used it. Does it work like the PowerBASIC formatter?


Karen Zibowski

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

Brian Alvarez

Yes, please put an example together, seems like a feasible solution. :)

Charles Pegge

#13
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

Mike Lobanovsky

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?
Mike
(3.6GHz Intel Core i5 w/ 16GB RAM, 2 x GTX 650Ti w/ 2GB VRAM, Windows 7 Ultimate Sp1)