News:

Function Finder  Find all the functions within source code files

Main Menu

Confused by floating numbers

Started by Tifu, February 20, 2009, 05:10:05 PM

Previous topic - Next topic

Tifu

After trying to get some code working, I isolated what seemed to be the problem into its own file...
; sets up the screen
SetFPS 60
Mouse Off

DO
  cls rgb(0,0,0)
  delay=delay-1
  if delay<0 then x#=x#+0.1: delay=20
  text 10,10,str$(x#)
  sync
LOOP

This is the entirity of the code.
I am getting outputs that I didn't expect. "3.199999" and such. Shouldn't that be impossible? I should only get 3.1, 3.2, 3.3... only one number after the decimal... surely?

This is the results i'm getting:


I am confused? Help please D:

Compiler version is apparently 1.64h

kevin

#1
QuoteI am getting outputs that I didn't expect. "3.199999" and such. Shouldn't that be impossible? I should only get 3.1, 3.2, 3.3... only one number after the decimal... surely?

Nope, see   Variable Casting and Math operations  (How precise are floating Point values in PlayBasic ?)  

  Your example is getting cumulative error,  as floats can't represent all the possible values in a 32bit single.   A common approach for such calculations, is to do the cumulative additions as Integers, then convert to the back to float via division.  This removes the effects of the float precision (the lost bits, aka the error) creeping into the calculation and being magnified over time.


PlayBASIC Code: [Select]
 for lp=1 to 20
x=x+1
print str$(x/10.0)
next
Sync
waitkey





   


Tifu

OK... was very confused there. Thanks for clearing that up :)