It just doesn't add up....

General FreeBASIC programming questions.
Post Reply
kleelof
Posts: 17
Joined: Apr 29, 2006 7:08

It just doesn't add up....

Post by kleelof »

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.
stylin
Posts: 1253
Joined: Nov 06, 2005 5:19

Post by stylin »

I would start explicitly declaring my variables from now on: using, for example, dim as single n before I use it. Using option explicit reminds me to do this as well.
Sisophon2001
Posts: 1706
Joined: May 27, 2005 6:34
Location: Cambodia, Thailand, Lao, Ireland etc.
Contact:

Post by Sisophon2001 »

The default data type in FB is integer. In QB it was single. If you add

Code: Select all

dim n as single
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.
Veggiet
Posts: 156
Joined: Apr 17, 2006 19:41

Post by Veggiet »

Operator overloading would be great, especially with custom types :)
jevans4949
Posts: 1186
Joined: May 08, 2006 21:58
Location: Crewe, England

Ancient Basic Default

Post by jevans4949 »

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!
jdebord
Posts: 547
Joined: May 27, 2005 6:20
Location: Limoges, France
Contact:

Post by jdebord »

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.
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.

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
The second result is erroneous because 0.1 has an infinite number of decimals in base 2, while 0.5 is a power of 2 and therefore has a finite binary representation.
jdebord
Posts: 547
Joined: May 27, 2005 6:20
Location: Limoges, France
Contact:

Re: Ancient Basic Default

Post by jdebord »

jevans4949 wrote:In BASIC, Variables starting i thru n are by default integers, all others floating point.
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
jevans4949
Posts: 1186
Joined: May 08, 2006 21:58
Location: Crewe, England

Re: Ancient Basic Default

Post by jevans4949 »

[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.
Dirk_Fist
Posts: 82
Joined: Mar 07, 2006 11:28

Post by Dirk_Fist »

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

Code: Select all

dim as integer i,j
dim as single  x 

i=10
x=.5
? i+x
j=i+x
?j
Post Reply