Request: Double data types

Blitz3D Forums/Blitz3D Bug Reports/Request: Double data types

The forum is locked. These are the original blitzbasic.com forums, kept so they can still be read. No new topics, replies or edits can be made here. To join in, head over to the Blitz3D+ forums.

Well, I have a new function, this once's my favorite because it's not so ugly. It won't work for values smaller than 1.0e-004 so I cheat after that and just return Abs(value#). I couldn't fix the inaccuracies in these very small values, but none of the other versions could do this anyway. If you get any errors let me know what range they're in.

;FloatFraction

Graphics 640, 480, 0, 2

CheckSomeValues = 0

If CheckSomeValues
 For loop = 1 To 5
  Select loop
   Case 1 : value# = -0.000000000000000000000000000000000000000000001
   Case 2 : value# = -100000000000000000000000000000000000000.0
   Case 3 : value# = -1.00126
   Case 4 : value# = -0.00000000126
   Case 5 : value# = -370001.6
  End Select
  fraction# = FloatFraction(value#)
  Print "value# = " + value# + ", fraction# = " + fraction#
  Next
 WaitKey()
 End
EndIf

SeedRnd MilliSecs()

For pow = -15 To 15
 For loop = 0 To 10000
  value# = Rnd(-9 * (10 ^ pow), 9 * (10 ^ pow))
  valuestr$ = Float2Str(value#, 1)
  fraction# = FloatFraction(value#)
  fractionstr$ = Float2Str(fraction#, 0)
  If valuestr$ <> fractionstr$
   Print "value# = " + value# + ", fraction# = " + fraction#
   Print " valuestr$ = " + valuestr$ + ", fractionstr$ = " + fractionstr$
   errors = errors + 1
  EndIf
 Next
 Print errors + " errors" + " when pow = " + pow
 errors = 0
Next

Print "done"
WaitKey()
End

Function FloatFraction#(Value#)
 ;Returns the fractional part of a float as a float
 ;The whole part and sign are removed

 Local Number#, Result#, Loop, StartExp
 Local EndExp, Pow#, Fraction#, Round#

 Number# = Abs(Value#) ;Remove the sign
 Result# = 0.1
 For Loop = 1 To 38 ;Get whole places
  If Result# * 10 => Number# Then Exit
  Result# = Result# * 10
  StartExp = StartExp + 1
 Next

 If StartExp = 0 ;Value less than 1
  Result# = Number#
  For Loop = 1 To 45 ;Get fractional places
   Result# = Result# * 10
   If Int(Floor(Result#)) > 0 Then Exit
   StartExp = StartExp - 1
  Next
 EndIf

 EndExp = StartExp - 6
 Pow# = 1
 For Loop = EndExp To -1 ;Get exponent
  Pow# = Pow# * 10 ;Instead of 10 ^ EndExp
 Next

 Fraction# = (Number# * Pow#) - (Floor(Number#) * Pow#)
 Round# = (Number# * Pow#) - (Floor(Number# * Pow#))

 If Int(Round# * 10) = 5 ;Value after last meaningful place
  If Round# >= 0.5
   Fraction# = Fraction# + 0.1 ;Round up
  Else
   Fraction# = Fraction# - 0.1 ;Round down
  EndIf
 EndIf

 Result# = Int(Fraction#) / Pow#
 If Result# >= 1.0 Then Result# = 0.0 ;Special case rounding error
 If StartExp > 37 Then Result# = 0.0 ;Special case for Infinity
 If StartExp < -4 Then Result# = Number# ;Cheat if value very small
 Return Result#

End Function

Function Float2Str$(Value#, RemoveWhole = 0)
 ;Converts a float into a string
 ;RemoveWhole -> Remove whole number flag, 0 = No, 1 = Yes
 ;Removes the exponent form

 Local Number$, Position, Result$, Exponent, Loop, Char$

 Number$ = Str(Abs(Value#))
 Result$ = Number$
 If RemoveWhole
  Position = Instr(Number$, ".", 1)
  If Position
   Result$ = "0." + Right(Number$, Len(Number$) - Position)
  EndIf
 EndIf

 Position = Instr(Number$, "e-", 1)
 If Position
  Result$ = "0."
  Exponent = Right(Number$, Len(Number$) - Position - 1)
  For Loop = 1 To Exponent - 1
   Result$ = Result$ + "0"
  Next
  For Loop = 1 To Len(Number$)
   Char$ = Mid(Number$, Loop, 1)
   If Char$ = "e" Then Exit
   If Char$ <> "." Then Result$ = Result$ + Char$
  Next
 EndIf

 Position = Instr(Number$, "e+", 1)
 If Position
  Result$ = ""
  Exponent = Right(Number$, Len(Number$) - Position - 1)
  For Loop = 1 To Len(Number$)
   Char$ = Mid(Number$, Loop, 1)
   If Char$ = "e" Then Exit
   If Char$ <> "." Then Result$ = Result$ + Char$
  Next
  For Loop = 1 To Exponent - 1
   Result$ = Result$ + "0"
  Next
  Result$ = Result$ + ".0"
  If RemoveWhole Then Result$ = "0.0"
 EndIf

 Return Result$

End Function