Converting a string to a double problem
- TheAutomator
- Enthusiast
- Posts: 112
- Joined: Tue Dec 01, 2020 8:33 pm
Converting a string to a double problem
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...?
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!
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...?
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.
Re: Converting a string to a double problem
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.
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
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
- TheAutomator
- Enthusiast
- Posts: 112
- Joined: Tue Dec 01, 2020 8:33 pm
Re: Converting a string to a double problem
okay seems logical but then explain the vb example?
what magic was going on there?
what magic was going on there?
Re: Converting a string to a double problem
VB probably cleans up the output automatically. Internally, VB would get 3.1400000000000001 as well.TheAutomator wrote:explain the vb example?
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.
VB holds the user's hand.TheAutomator wrote:what magic was going on there?
Last edited by BarryG on Tue Dec 01, 2020 10:01 pm, edited 2 times in total.
Re: Converting a string to a double problem
To be fair:
MsgBox needs a string.
So you need to convert the double back to a string:
MsgBox needs a string.
So you need to convert the double back to a string:
Code: Select all
Debug StrD(ValD("3.14"), 2)
- TheAutomator
- Enthusiast
- Posts: 112
- Joined: Tue Dec 01, 2020 8:33 pm
Re: Converting a string to a double problem
That's what 'debug' is doing in this exampleinfratec 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)
Last edited by TheAutomator on Sat Dec 05, 2020 9:07 pm, edited 2 times in total.
- TheAutomator
- Enthusiast
- Posts: 112
- Joined: Tue Dec 01, 2020 8:33 pm
Re: Converting a string to a double problem
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 differentBarryG wrote:VB probably cleans up the output automatically. Internally, VB would get 3.1400000000000001 as well.TheAutomator wrote:explain the vb example?
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.
VB holds the user's hand.TheAutomator wrote:what magic was going on there?
- TheAutomator
- Enthusiast
- Posts: 112
- Joined: Tue Dec 01, 2020 8:33 pm
Re: Converting a string to a double problem
I think this might be a workaround :
Code: Select all
double.s = "3.14"
Debug StrD(ValD(double), Len(StringField(double, 2, ".")))
- VB6_to_PBx
- Enthusiast
- Posts: 625
- Joined: Mon May 09, 2011 9:36 am
Re: Converting a string to a double problem
your workaround example seems to work wellTheAutomator wrote:I think this might be a workaround :
Code: Select all
double.s = "3.14" Debug StrD(ValD(double), Len(StringField(double, 2, ".")))
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
Re: Converting a string to a double problem
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!
- NicTheQuick
- Addict
- Posts: 1227
- Joined: Sun Jun 22, 2003 7:43 pm
- Location: Germany, Saarbrücken
- Contact:
Re: Converting a string to a double problem
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.TheAutomator wrote:I'm writing an interpreter in PureBasic and converting tokens from a text file / string to the accurate numbers is a must
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.
- TheAutomator
- Enthusiast
- Posts: 112
- Joined: Tue Dec 01, 2020 8:33 pm
Re: Converting a string to a double problem
uhm, that's just numerical overflow for a double right there, try:VB6_to_PBx wrote:TheAutomator wrote:I think this might be a workaround :
your workaround example seems to work well
Code: Select all
EnableExplicit Define.s double = "3.1415926535897932384626433832795" Debug StrD(ValD(double), Len(StringField(double, 2, ".")))
Code: Select all
Define double.d = 3.1415926535897932384626433832795
- TheAutomator
- Enthusiast
- Posts: 112
- Joined: Tue Dec 01, 2020 8:33 pm
Re: Converting a string to a double problem
well, if you would make an interpreter, it has to read text and convert it to tokens right?NicTheQuick wrote: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.TheAutomator wrote:I'm writing an interpreter in PureBasic and converting tokens from a text file / string to the accurate numbers is a must
for example:
contents of a script file:
Code: Select all
test = 3.14
msgbox test
Code: Select all
"test" = identifier
"=" = operator
"3.14" = double -> covert to actual number in memory!
"crlf" = newline
"msgbox" = function
"test" = identifier
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?
Re: Converting a string to a double problem
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
Wiki: https://en.wikipedia.org/wiki/Double-pr ... int_format
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)
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
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
Re: Converting a string to a double problem
Does this help?TheAutomator wrote:vbscript:
msgbox Cdbl("3.14") ' OUTPUT = '3.14'
Code: Select all
Debug StrD(ValD("3.14")) ; Outputs 3.14 like VBScript