Converting a string to a double problem

Just starting out? Need help? Post your questions and find answers here.
User avatar
TheAutomator
Enthusiast
Enthusiast
Posts: 112
Joined: Tue Dec 01, 2020 8:33 pm

Converting a string to a double problem

Post by TheAutomator »

Hello! :)
my first post here, I'll keep the description of the problem simple:

vbscript:

msgbox Cdbl("3.14") ' OUTPUT = '3.14'

purebasic:

Debug ValD("3.14") ; OUTPUT = '3.1400000000000001' wut...? :shock:

I'm writing an interpreter in PureBasic and converting tokens from a text file / string to the accurate numbers is a must...
Does anyone know how to do this correctly?

Regards!
Last edited by TheAutomator on Sat Dec 05, 2020 2:51 am, edited 1 time in total.
User avatar
mk-soft
Always Here
Always Here
Posts: 5409
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: Converting a string to a double problem

Post by mk-soft »

A float or a double is a structured number

A smooth 1.0 does not exist. Look at wiki how a float or double is stored in memory.
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
User avatar
TheAutomator
Enthusiast
Enthusiast
Posts: 112
Joined: Tue Dec 01, 2020 8:33 pm

Re: Converting a string to a double problem

Post by TheAutomator »

okay seems logical but then explain the vb example?
what magic was going on there?
BarryG
Addict
Addict
Posts: 3330
Joined: Thu Apr 18, 2019 8:17 am

Re: Converting a string to a double problem

Post by BarryG »

TheAutomator wrote:explain the vb example?
VB probably cleans up the output automatically. Internally, VB would get 3.1400000000000001 as well.

In PureBasic, you can set the number of digits to show after the decimal point like this:

Code: Select all

Debug StrD(ValD("3.14"),5) ; 5 = Show 5 digits after the decimal point.
TheAutomator wrote:what magic was going on there?
VB holds the user's hand.
Last edited by BarryG on Tue Dec 01, 2020 10:01 pm, edited 2 times in total.
infratec
Always Here
Always Here
Posts: 6883
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: Converting a string to a double problem

Post by infratec »

To be fair:

MsgBox needs a string.
So you need to convert the double back to a string:

Code: Select all

Debug StrD(ValD("3.14"), 2)
User avatar
TheAutomator
Enthusiast
Enthusiast
Posts: 112
Joined: Tue Dec 01, 2020 8:33 pm

Re: Converting a string to a double problem

Post by TheAutomator »

infratec wrote:To be fair:

MsgBox needs a string.
So you need to convert the double back to a string:

Code: Select all

Debug StrD(ValD("3.14"), 2)
That's what 'debug' is doing in this example ;)
Last edited by TheAutomator on Sat Dec 05, 2020 9:07 pm, edited 2 times in total.
User avatar
TheAutomator
Enthusiast
Enthusiast
Posts: 112
Joined: Tue Dec 01, 2020 8:33 pm

Re: Converting a string to a double problem

Post by TheAutomator »

BarryG wrote:
TheAutomator wrote:explain the vb example?
VB probably cleans up the output automatically. Internally, VB would get 3.1400000000000001 as well.

In PureBasic, you can set the number of digits to show after the decimal point like this:

Code: Select all

Debug StrD(ValD("3.14"),5) ; 5 = Show 5 digits after the decimal point.
TheAutomator wrote:what magic was going on there?
VB holds the user's hand.
Damn, this is not going to be easy.. now i also need to find out how to dynamically find out how much digits to show if the value is different :?
User avatar
TheAutomator
Enthusiast
Enthusiast
Posts: 112
Joined: Tue Dec 01, 2020 8:33 pm

Re: Converting a string to a double problem

Post by TheAutomator »

I think this might be a workaround :idea: :

Code: Select all

double.s = "3.14"
Debug StrD(ValD(double), Len(StringField(double, 2, ".")))
User avatar
VB6_to_PBx
Enthusiast
Enthusiast
Posts: 625
Joined: Mon May 09, 2011 9:36 am

Re: Converting a string to a double problem

Post by VB6_to_PBx »

TheAutomator wrote:I think this might be a workaround :idea: :

Code: Select all

double.s = "3.14"
Debug StrD(ValD(double), Len(StringField(double, 2, ".")))
your workaround example seems to work well

Code: Select all

EnableExplicit
Define.s double = "3.1415926535897932384626433832795"
Debug StrD(ValD(double), Len(StringField(double, 2, ".")))
 
PureBasic .... making tiny electrons do what you want !

"With every mistake we must surely be learning" - George Harrison
User avatar
C87
Enthusiast
Enthusiast
Posts: 176
Joined: Mon Jul 17, 2017 7:22 am
Location: Cotswolds England

Re: Converting a string to a double problem

Post by C87 »

If I want to remain in control of the result I only use integer calcs. :)
If it's falling over......just remember the computer is never wrong!
User avatar
NicTheQuick
Addict
Addict
Posts: 1227
Joined: Sun Jun 22, 2003 7:43 pm
Location: Germany, Saarbrücken
Contact:

Re: Converting a string to a double problem

Post by NicTheQuick »

TheAutomator wrote:I'm writing an interpreter in PureBasic and converting tokens from a text file / string to the accurate numbers is a must
I don't know what you really mean with "accurate numbers". If it is what I understand under "accurate numbers" then floats and doubles are nothing you should use. Especially for financial calculus you should use the smallest unit and store them as an integer. For example instead of storing 1.23 € as a float number with the unit €, just store 123 as an integer with the unit ¢ or €/100. This way you can not get weird values in between.
The english grammar is freeware, you can use it freely - But it's not Open Source, i.e. you can not change it or publish it in altered way.
User avatar
TheAutomator
Enthusiast
Enthusiast
Posts: 112
Joined: Tue Dec 01, 2020 8:33 pm

Re: Converting a string to a double problem

Post by TheAutomator »

VB6_to_PBx wrote:
TheAutomator wrote:I think this might be a workaround :idea: :
your workaround example seems to work well

Code: Select all

EnableExplicit
Define.s double = "3.1415926535897932384626433832795"
Debug StrD(ValD(double), Len(StringField(double, 2, ".")))
uhm, that's just numerical overflow for a double right there, try:

Code: Select all

Define double.d = 3.1415926535897932384626433832795
User avatar
TheAutomator
Enthusiast
Enthusiast
Posts: 112
Joined: Tue Dec 01, 2020 8:33 pm

Re: Converting a string to a double problem

Post by TheAutomator »

NicTheQuick wrote:
TheAutomator wrote:I'm writing an interpreter in PureBasic and converting tokens from a text file / string to the accurate numbers is a must
I don't know what you really mean with "accurate numbers". If it is what I understand under "accurate numbers" then floats and doubles are nothing you should use. Especially for financial calculus you should use the smallest unit and store them as an integer. For example instead of storing 1.23 € as a float number with the unit €, just store 123 as an integer with the unit ¢ or €/100. This way you can not get weird values in between.
well, if you would make an interpreter, it has to read text and convert it to tokens right?
for example:

contents of a script file:

Code: Select all

test = 3.14
msgbox test
the interpreter makes tokens from it:

Code: Select all

"test" = identifier
"=" = operator
"3.14" = double -> covert to actual number in memory!
"crlf" = newline
"msgbox" = function
"test" = identifier
wouldn't it be weird if you execute this script and it shows a message box saying 3.1400000000000001?
I'm looking for a way to correctly do this...

Your suggestion is to store 3.14 as 314 and only add a dot and convert it to a float if you need to?
how would that work?
User avatar
mk-soft
Always Here
Always Here
Posts: 5409
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: Converting a string to a double problem

Post by mk-soft »

Not yet looked up how double values are stored (wiki)

It is your task to display the double value at the output with the desired number of decimal places

Code: Select all

dblVal.d = 1.101
Debug "Unformated"
Debug dblVal
Debug "Formated"
Debug StrD(dblVal, 3)
Wiki: https://en.wikipedia.org/wiki/Double-pr ... int_format
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
BarryG
Addict
Addict
Posts: 3330
Joined: Thu Apr 18, 2019 8:17 am

Re: Converting a string to a double problem

Post by BarryG »

TheAutomator wrote:vbscript:
msgbox Cdbl("3.14") ' OUTPUT = '3.14'
Does this help?

Code: Select all

Debug StrD(ValD("3.14")) ; Outputs 3.14 like VBScript
Post Reply