Author Topic: Variables not behaving as you would think..  (Read 2605 times)

0 Members and 2 Guests are viewing this topic.

kryton9

  • Guest
Variables not behaving as you would think..
« on: June 30, 2011, 09:17:27 PM »
Code: OxygenBasic
  1. string s$ = ""
  2.         '9,223,372,036.854,775,807
  3. double n = 9223372036.854775807
  4. s$ += "double: trun = " str(trunc(n)) "      frac = " str(frac(n))  chr(10) chr(13)
  5. print s$
  6.  
  7. any a = n
  8. s$ += "any: trun = "    str(trunc(a)) "      frac = " str(frac(a))  chr(10) chr(13)
  9. print s$
  10.  
  11. float f = n
  12. s$ += "float: trun = " str(trunc(f)) "      frac = " str(frac(f))  chr(10) chr(13)
  13. print s$
  14.  
  15. single s = n
  16. s$ += "single: trun = " str(trunc(s)) "      frac = " str(frac(s))  chr(10) chr(13)
  17. print s$

Charles Pegge

  • Guest
Re: Variables not behaving as you would think..
« Reply #1 on: July 01, 2011, 02:37:19 AM »
Kent,
The precision limit is about 17 digits for Doubles. So the internal str format imposes this limit.

Code: OxygenBasic
  1. s="1.2345678901234567890"
  2. n=val s
  3. print s "
  4. " str(n) "
  5. " str(trunc(n)) "
  6. " str(frac (n)) "
  7. "
  8.  

Higher precision maths requires more specialised techniques. We can create classes for high digit numbers with their own operators.

I have one for vectors in this folder:

examples/oop/operators

Charles

kryton9

  • Guest
Re: Variables not behaving as you would think..
« Reply #2 on: July 01, 2011, 02:44:44 AM »
I normally don't use big numbers as these but when I saw you mentioned ai and science modules, I figure they might need such numbers.

the any variable surprised me too, not sure, it looks like it doesn't do any only certain ones :)

About double, coming from c world... here is definition I have.
This format gives a range of approximately 1.7E–308 to 1.7E+308 for type double.

So confusing, that is why that chart I posted is important when you get a chance to fill that out. I know you know the insides and outs of
all of this, but we mere mortals don't have that knowledge :)

Charles Pegge

  • Guest
Re: Variables not behaving as you would think..
« Reply #3 on: July 01, 2011, 04:19:54 AM »

Any is a sort of dummy - which I make equivalent to the sys type. One would not normally use it directly.

In Basic byref any is a substitute for C void*

Yes Double numbers have a scale range much greater than the granularity of the known universe, but the precision if more limited. You can still go a long way down into fractals with Doubles before hitting precision artefacts.

Charles

kryton9

  • Guest
Re: Variables not behaving as you would think..
« Reply #4 on: July 01, 2011, 11:16:42 AM »
Oh ok, so I shouldn't be so worried about it. Good to know.