Numbers in Scientific Format, unexpected output.

General FreeBASIC programming questions.
Richard
Posts: 2953
Joined: Jan 15, 2007 20:44
Location: Australia

Numbers in Scientific Format, unexpected output.

Postby Richard » Mar 29, 2007 20:18

Formatting numbers is important because it gets them through the eyes and into the brain. The following code shows unexpected output from both "print using" and "format" keywords.

Code: Select all

#include "vbcompat.bi"
Dim As String result, formatting_expression = "000E+00"
Dim As Double numerical_expression = -1.23456789e-7
Dim As Integer i

Print " i   format string = "; formatting_expression

For i = 1 To 20
    result = Format( numerical_expression, formatting_expression )
    Print Using "###   +#.#####^^^^    &"; i, numerical_expression; result
    numerical_expression *= 10
Next i

Sleep
The above code gave the following output.

Code: Select all

i   format string = 000E+00
  1   -1.23457e-07    -123E-09
  2   -1.23457e-06    -123E-08
  3   -0.00001e+00    -123E-07
  4   -0.00012e+00    -123E-06
  5   -0.00123e+00    -123E-05
  6   -0.01235e+00    -123E-04
  7   -0.12346e+00    -123E-03
  8   -1.23457e+00    -001E+00
  9   -1.23457e+01    -012E+00
 10   -1.23457e+02    -123E+00
 11   -1.23457e+03    -1235E+00
 12   -1.23457e+04    -12346E+00
 13   -1.23457e+05    -123457E+00
 14   -1.23457e+06    -1234568E+00
 15   -1.23457e+07    -12345679E+00
 16   -1.23457e+08    -123456789E+00
 17   -1.23457e+09    -1234567890E+00
 18   -1.23457e+10    -12345678900E+00
 19   -1.23457e+11    -123456789000E+00
 20   -1.23457e+12    -1234567890000E+00
With "print using", even though we have scientific mode, when i is 3, 4, 5, 6 and 7 we have lost information.
With "Format" when i is 8 and 9 we have lost information but from 11 we get too much information, outside the specified format. Are these bugs or am I expecting too much?
Although it is possible to work round the "Format" problem in some cases by placing a decimal point in the mantissa format, it cannot be automated because of the unusual behavior and the point then appears in the output string. There is no requirement that a point appear in the mantissa.
I suspect both these problems come from considering the exponent before the mantissa in the formatting process.
"Format" produces a variable length string because there is no control of the leading sign. Negative numbers are one character longer than positive. This may need to be handled by the programmer, whereas with "Print Using" we can force a sign at the start to fix the length.

Return to “General”

Who is online

Users browsing this forum: Google [Bot] and 5 guests