Discussion:
Can we trust Powerbasic math?
(too old to reply)
Olav
2010-06-17 06:46:15 UTC
Permalink
Raw Message
In the first calculation shown below both Visual Basic, my calculator and
even Windows calculator produce a result of 5 720 000 000. Also Wikipedia
claims it should be 5 720 000 000.
http://en.wikipedia.org/wiki/Scientific_notation

Is E9 and 10^9 two different worlds in Powerbasic?

FUNCTION PBMAIN () AS LONG

LOCAL a AS QUAD

a = 5.72 * 10^9 ' outputs 5 719 999 790
? a
a = 5.72E9 ' outputs 5 720 000 000
? a
? "Press a key to end the program..."
WAITKEY$

END FUNCTION
Greg Neill
2010-06-17 10:54:16 UTC
Permalink
Raw Message
Post by Olav
In the first calculation shown below both Visual Basic, my calculator and
even Windows calculator produce a result of 5 720 000 000. Also Wikipedia
claims it should be 5 720 000 000.
http://en.wikipedia.org/wiki/Scientific_notation
Is E9 and 10^9 two different worlds in Powerbasic?
FUNCTION PBMAIN () AS LONG
LOCAL a AS QUAD
a = 5.72 * 10^9 ' outputs 5 719 999 790
? a
a = 5.72E9 ' outputs 5 720 000 000
? a
? "Press a key to end the program..."
WAITKEY$
END FUNCTION
In the first instance you have a single precision
constant multiplied by 10^9, then the type of the
result will be converted for the assignment to a.

In the second case you have a constant 5.72E9, with
no mathematical operations, being promoted in
precision at compile time for the assignment.

In place of your first assignment statement, try
specifying an apppropriate precision for the constant:

a = 5.72# * 10^9
Olav
2010-06-17 11:23:02 UTC
Permalink
Raw Message
Post by Greg Neill
Post by Olav
In the first calculation shown below both Visual Basic, my calculator and
even Windows calculator produce a result of 5 720 000 000. Also Wikipedia
claims it should be 5 720 000 000.
http://en.wikipedia.org/wiki/Scientific_notation
Is E9 and 10^9 two different worlds in Powerbasic?
FUNCTION PBMAIN () AS LONG
LOCAL a AS QUAD
a = 5.72 * 10^9 ' outputs 5 719 999 790
? a
a = 5.72E9 ' outputs 5 720 000 000
? a
? "Press a key to end the program..."
WAITKEY$
END FUNCTION
In the first instance you have a single precision
constant multiplied by 10^9, then the type of the
result will be converted for the assignment to a.
In the second case you have a constant 5.72E9, with
no mathematical operations, being promoted in
precision at compile time for the assignment.
In place of your first assignment statement, try
a = 5.72# * 10^9
Yes, thank you. You are right, but Visual Basic 2010. and calculators,
solve this in a more elegant way.

Maybe VB, and calculators, sees the value 5.72 as an integer value equal
572.You then will have:

(572 * 10^9) /100 = 5 720 000 000

VB code:
Dim a As ULong
a = 5.72 * 10 ^ 9

Console.WriteLine(a) ' outputs 5 720 000 000

--

Olav
WangoTango
2010-06-17 15:22:03 UTC
Permalink
Raw Message
Post by Olav
Post by Greg Neill
Post by Olav
In the first calculation shown below both Visual Basic, my calculator and
even Windows calculator produce a result of 5 720 000 000. Also Wikipedia
claims it should be 5 720 000 000.
http://en.wikipedia.org/wiki/Scientific_notation
Is E9 and 10^9 two different worlds in Powerbasic?
FUNCTION PBMAIN () AS LONG
LOCAL a AS QUAD
a = 5.72 * 10^9 ' outputs 5 719 999 790
? a
a = 5.72E9 ' outputs 5 720 000 000
? a
? "Press a key to end the program..."
WAITKEY$
END FUNCTION
In the first instance you have a single precision
constant multiplied by 10^9, then the type of the
result will be converted for the assignment to a.
In the second case you have a constant 5.72E9, with
no mathematical operations, being promoted in
precision at compile time for the assignment.
In place of your first assignment statement, try
a = 5.72# * 10^9
Yes, thank you. You are right, but Visual Basic 2010. and calculators,
solve this in a more elegant way.
I think you are confusing the ability to control the precision of your
data as a short coming. I know it is BASIC, but come on.
Post by Olav
Maybe VB, and calculators, sees the value 5.72 as an integer value equal
Fixed point math is common, but I doubt VB would use it. More likely
they default to the highest precision. All of this should be something
found in the documentation of the compiler, including the PowerBASIC
"problem".
Post by Olav
(572 * 10^9) /100 = 5 720 000 000
Dim a As ULong
a = 5.72 * 10 ^ 9
Console.WriteLine(a) ' outputs 5 720 000 000
hutch--
2010-06-18 08:38:01 UTC
Permalink
Raw Message
Pick your precision level.

Results

1 integer
5719999790
5720000000

2 extended
5719999790.19165039
5720000000

3 double
5719999790.19165039
5720000000

4 single
5720000000
5720000000

code

' ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤

FUNCTION PBmain as LONG

LOCAL a AS QUAD

LOCAL x as EXT
LOCAL y as DOUBLE
LOCAL z as SINGLE

StdOut chr$(13,10)+"1 integer"
a = 5.72 * 10^9 ' outputs 5 719 999 790
? a
a = 5.72E9 ' outputs 5 720 000 000
? a

StdOut chr$(13,10)+"2 extended"
x = 5.72 * 10^9
StdOut str$(x,24)
x = 5.72E9
StdOut str$(x,24)

StdOut chr$(13,10)+"3 double"
y = 5.72 * 10^9
StdOut str$(y,24)
y = 5.72E9
StdOut str$(y,24)

StdOut chr$(13,10)+"4 single"
z = 5.72 * 10^9
StdOut str$(z,24)
z = 5.72E9
StdOut str$(z,24)

Do
Sleep 1
Loop while inkey$ = ""

FUNCTION = 0

End FUNCTION

' ¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤¤
Loading...