Quote: ""I'll also try experimenting with a "string to float" function, do you think something similar would work for converting them?"
Yup. It's a little trickier though. "
It was, but I've done it
Rem * Title : String To Float
Rem * Author : Robert the Robot
Rem * Date : 24/2/08
rem ****************************
rem Converts A String To A Float
rem ****************************
Print Str_To_Float("1.89")
end
Function Str_To_Float(String$)
rem If negative then set flag, and remove first character
If Mid$(String$, 1) = "-"
NegFlag=1
New$ = Right$(String$, Len(String$)-1)
else
New$ = String$
endif
rem Work out the integer part of the String
T = 1
While T<Len(New$)
If Mid$(New$, T) = "."
Int$ = Left$(New$, T-1)
Exit
else
Inc T
endif
EndWhile
rem Set up variables
StringLength = Len(Int$)
NewFloat# = 0
Power = StringLength
rem Generate integer part of float
For N = 1 to StringLength
Inc NewFloat#,(ASC(Mid$(Int$, N))-48)*(10^(Power-1))
Dec Power, 1
next N
rem Set up variables again
Power# = 0.1
rem generate float part
For N = Len(Int$)+2 to Len(String$)
Inc NewFloat#,(ASC(Mid$(New$, N))-48)*(Power#)
Power# = Power#/10
next N
If NegFlag=1 then NewFloat# = 0-NewFloat#
EndFunction NewFloat#
with number validation:
Rem * Title : String To Float
Rem * Author : Robert the Robot
Rem * Date : 24/2/08
rem ****************************************************
rem Converts a String of Text to a Float with validation
rem ****************************************************
Print Str_To_Float("1.89")
end
Function Str_To_Float(String$)
rem If negative then set flag, and remove first character
If Mid$(String$, 1) = "-"
NegFlag=1
Temp$ = Right$(String$, Len(String$)-1)
else
Temp$ = String$
endif
rem loop through the values
StringLength = Len(Temp$)
For N = 1 to StringLength
TempNo = Asc(Mid$(Temp$, N))
If TempNo >= 46 and TempNo <= 57 and TempNo<>47
New$ = New$ + Mid$(Temp$, N)
else
ExitFunction 0
endif
Next N
rem Work out the integer part of the String
T = 1
While T<Len(New$)
If Mid$(New$, T) = "."
Int$ = Left$(New$, T-1)
Exit
else
Inc T
endif
EndWhile
rem Set up variables
StringLength = Len(Int$)
NewFloat# = 0
Power = StringLength
rem Generate integer part of float
For N = 1 to StringLength
Inc NewFloat#,(ASC(Mid$(Int$, N))-48)*(10^(Power-1))
Dec Power, 1
next N
rem Set up variables again
Power# = 0.1
rem generate float part
For N = Len(Int$)+2 to Len(String$)
Inc NewFloat#,(ASC(Mid$(New$, N))-48)*(Power#)
Power# = Power#/10
next N
If NegFlag=1 then NewFloat# = 0-NewFloat#
EndFunction NewFloat#
and for an integer:
Rem * Title : String To Int
Rem * Author : Robert the Robot
Rem * Date : 24/2/08
rem *******************************
rem Converts A String To An Integer
rem *******************************
Print str_to_int("214743647")
end
Function Str_To_Int(String$)
rem If negative then set flag, and remove first character
If Mid$(String$, 1) = "-"
NegFlag=1
New$ = Right$(String$, Len(String$)-1)
else
New$ = String$
endif
rem Set up variables
StringLength = Len(New$)
NewInteger = 0
Power = StringLength
For N = 1 to StringLength
rem Inc the final number, by a number raised to the power of (nchars -1)
Inc NewInteger,(ASC(Mid$(New$, N))-48)*(10^(Power-1))
Dec Power, 1
next N
If NegFlag=1 then NewInteger = 0-NewInteger
EndFunction NewInteger
and again, with validation (to make sure it is a pure number):
Rem * Title : String To Int
Rem * Author : Robert the Robot
Rem * Date : 24/2/08
rem *******************************************************
rem Converts A String To An Integer with a validation check
rem *******************************************************
Print str_to_int("214743647")
end
Function Str_To_Int(String$)
rem If negative then set flag, and remove first character
If Mid$(String$, 1) = "-"
NegFlag=1
Temp$ = Right$(String$, Len(String$)-1)
else
Temp$ = String$
endif
rem loop through the values
StringLength = Len(Temp$)
For N = 1 to StringLength
TempNo = Asc(Mid$(Temp$, N))
If TempNo >= 46 and TempNo <= 57 and TempNo<>47
New$ = New$ + Mid$(Temp$, N)
else
ExitFunction 0
endif
Next N
rem Set up variables
StringLength = Len(New$)
NewInteger = 0
Power = StringLength
For N = 1 to StringLength
rem Inc the final number, by a number raised to the power of (nchars -1)
Inc NewInteger,(ASC(Mid$(New$, N))-48)*(10^(Power-1))
Dec Power, 1
next N
If NegFlag=1 then NewInteger = 0-NewInteger
EndFunction NewInteger
Quote: "DBC only has single precision (32 bit signed). It seems you can have up to 45 decimal places but the number returned is rounded to 6 digits and displayed in scientific notation."
Does that mean that floats - when they exist as floats - are formatted to 6 digits, or are they merely rounded to 6 digits when you convert it to a string for display on screen? And if that is the case, would it be possible to access the individual bits making up the float? I can read off each byte of the float by writing it to a memblock and reading the integer component, but I'd need to know what each bit actually signified.
Or am I just being picky, and none of this really matters at all
?
Did you hear about the two criminals who crashed their getaway car into a cement mixer? Police are looking for two hardened criminals...