Any ideas why the following code does not work?
n = .5 + .5
for z = 1 to 10: n = n + .5:print n:next
sleep
It should produce a list of numbers from 1.5 to 6, but instead returns a list of 2's.
however,
n = .5+.5
print n
sleep
returns a 1.
any ideas?
thanks.
It just doesn't add up....
-
- Posts: 1706
- Joined: May 27, 2005 6:34
- Location: Cambodia, Thailand, Lao, Ireland etc.
- Contact:
The default data type in FB is integer. In QB it was single. If you add
you will get the results you expected.
Adding 0.5 to an integers will provide results that I would not care to predict because of the way floating point numbers are stored. Even whole numbers stored in floating point format are not exact. All I can say is that you get the same result in VBA.
Garvan
EDIT:
I missed stylin reply.
Code: Select all
dim n as single
Adding 0.5 to an integers will provide results that I would not care to predict because of the way floating point numbers are stored. Even whole numbers stored in floating point format are not exact. All I can say is that you get the same result in VBA.
Garvan
EDIT:
I missed stylin reply.
-
- Posts: 1186
- Joined: May 08, 2006 21:58
- Location: Crewe, England
Ancient Basic Default
In BASIC, Variables starting i thru n are by default integers, all others floating point. This convention was adopted from FORTRAN, and has been standard, if not from the beginning, then at least before 1968 when I first learned it.
FreeBASIC also supports the standard overrides of % for INTEGER and ! for SINGLE (although deprecated). In the original specimen specify z% and n!
FreeBASIC also supports the standard overrides of % for INTEGER and ! for SINGLE (although deprecated). In the original specimen specify z% and n!
With floating point numbers, the results will depend on the binary representation. If all binary digits do not fit in memory, the results will be erroneous.Sisophon2001 wrote: Adding 0.5 to an integers will provide results that I would not care to predict because of the way floating point numbers are stored.
Code: Select all
option explicit
dim as integer i
dim as single x
x = 0
for i = 1 to 1000
x = x + 0.5
next i
? x
x = 0
for i = 1 to 1000
x = x + 0.1
next i
? x
Re: Ancient Basic Default
If I understand correctly, all variables are integers by default in FB and singles in QB. To modify this you have to define your types explicitly, with for instance DEFINT I-N or DIM'sjevans4949 wrote:In BASIC, Variables starting i thru n are by default integers, all others floating point.
-
- Posts: 1186
- Joined: May 08, 2006 21:58
- Location: Crewe, England
Re: Ancient Basic Default
[quote="jdebord]If I understand correctly, all variables are integers by default in FB and singles in QB. To modify this you have to define your types explicitly, with for instance DEFINT I-N or DIM's[/quote]
My sincere apologies, jdebord is right.
My only excuse is that most of my BASIC was done in pre-QBASIC days on versions that did follow that convention. I thought GW-Basic did ... will dust down my DOS machine tomorrow and check.
My sincere apologies, jdebord is right.
My only excuse is that most of my BASIC was done in pre-QBASIC days on versions that did follow that convention. I thought GW-Basic did ... will dust down my DOS machine tomorrow and check.
Adding a floating point number to an integer should result in the integer being converted to floating point
assigning a floating point to an integer should result in a conversion to floating point
so to answer sisophon
adding a floating point number to an integer will produce a result correct to the limits of the binary representation of single (or double)
assigning back to an integer will truncate the result back to an integer
assigning a floating point to an integer should result in a conversion to floating point
so to answer sisophon
adding a floating point number to an integer will produce a result correct to the limits of the binary representation of single (or double)
assigning back to an integer will truncate the result back to an integer
Code: Select all
dim as integer i,j
dim as single x
i=10
x=.5
? i+x
j=i+x
?j