Author Topic: Any reliable way to tell if a numbe is an integer in BASIC?  (Read 3659 times)

0 Members and 1 Guest are viewing this topic.

Halifax(DuplicateAcct)

  • Guest
Any reliable way to tell if a numbe is an integer in BASIC?
« on: November 17, 2009, 11:13:45 am »
Hey Omnimaga.

Alright, so just about a week or two ago I was participating in this math contest in which calculator programming was allowed to solve the problem. More to the point, I was required to find if a number was a perfect cube or not (e.g. the cube root of the number is an integer). Basically all I had to do was take the fractional part, fPart() of the number and make sure that there was none. At the time, during the contest, I was under the belief that if there were no numbers after the decimal, fPart() would return 0. Sadly I was mistaken, it returns either 0, 1, or a small number such as 1*10^-12 or something.

I'm thinking this is a problem with the binary encoding they use for floating point numbers. So now, I take the fractional part of numbers and make sure they are less than a certain epsilon, but this isn't completely reliable.  :-\

So I was wondering, how do you guys deal with it?
« Last Edit: November 17, 2009, 12:34:51 pm by DJ Omnimaga »

Offline DJ Omnimaga

  • Clacualters are teh gr33t
  • CoT Emeritus
  • LV15 Omnimagician (Next: --)
  • *
  • Posts: 55942
  • Rating: +3154/-232
  • CodeWalrus founder & retired Omnimaga founder
    • View Profile
    • Dream of Omnimaga Music
Re: Any reliable way to tell if a numbe is an integer in BASIC?
« Reply #1 on: November 17, 2009, 12:24:24 pm »
That's kinda interesting, because if I do for example fPart(³ √(35)), it will return the decimal part of the number fine (in this case, 0.2710663102), and if for example I replaced 35 with 27, it would have no decimal and would return 0 fine. Basically f I wanted to check if the cube root of my number is an integer in a program, I would simply do something like:

Prompt N
If fPart(³ √(N))=0
Then
Disp "NOT INTEGER"
Else
Disp "INTEGER"
End
Or more optimized:

Prompt N
If fPart(³ √(N
Then
Disp "NOT INTEGER
Else
Disp "INTEGER

I am not sure if this is what you want to do, though, but it is what I kinda gather. If it is what you want to do and it still doesn't work, could it be that you ran some other programs on your calculator like an ASM game (ran through Ion, MirageOs or APPS menu) that could have messed up the calculator math results? I remember once my function Rand got extremly messed up
« Last Edit: November 17, 2009, 12:34:40 pm by DJ Omnimaga »
Now active at https://discord.gg/cuZcfcF (CodeWalrus server)

Offline Halifax

  • LV9 Veteran (Next: 1337)
  • *********
  • Posts: 1334
  • Rating: +2/-1
    • View Profile
    • TI-Freakware
Re: Any reliable way to tell if a numbe is an integer in BASIC?
« Reply #2 on: November 17, 2009, 03:01:48 pm »
Sorry about that, I had to register a second account because I forgot my password. DJ Omnimaga and I got it reset though so I'm good now.

But anyways, we talked more on AIM and found out that it really is a glitch and it will be added to the following list: http://ourl.ca/3687

If anyone else wants to replicate it just try fPart(³ ?(64)).
There are 10 types of people in this world-- those that can read binary, and those that can't.

Offline Galandros

  • LV9 Veteran (Next: 1337)
  • *********
  • Posts: 1140
  • Rating: +42/-10
    • View Profile
Re: Any reliable way to tell if a numbe is an integer in BASIC?
« Reply #3 on: November 17, 2009, 04:17:35 pm »
Instead of fpart(N) you can do:
N-int(N)
or
N-ipart(N

I haven't tested but give it a try.
Hobbing in calculator projects.

Offline Builderboy

  • Physics Guru
  • CoT Emeritus
  • LV13 Extreme Addict (Next: 9001)
  • *
  • Posts: 5673
  • Rating: +613/-9
  • Would you kindly?
    • View Profile
Re: Any reliable way to tell if a numbe is an integer in BASIC?
« Reply #4 on: November 17, 2009, 04:44:22 pm »
That is iquivilent to fPart and yields the same problems, you want to use round(#,8) or something allong those lines.

fPart(round(#,8 should do what you want.

Offline simplethinker

  • LV7 Elite (Next: 700)
  • *******
  • Posts: 695
  • Rating: +16/-5
  • snjwffl
    • View Profile
Re: Any reliable way to tell if a numbe is an integer in BASIC?
« Reply #5 on: November 17, 2009, 05:03:08 pm »
I'm thinking this is a problem with the binary encoding they use for floating point numbers.
Actually, floating point numbers aren't stored in binary but in binary coded decimal (BCD).  The format is similar to scientific notation.  The first byte stores the sign (and whether it's real/complex), the next stores the exponent, and the next seven store 14 of the decimal digits (two digits in each byte; note that only 10 of 14 digits are ever displayed).  For example, 3.1415926535897 is (ignoring the sign and exponent) stored as "31 41 59 26 53 58 97" (in hexadecimal).

The problem most likely lies in the computation of the cube root, which apparently is only accurate to 12-13 places (in this case), leaving at least one non-zero digit (for the cube root of 64, the calculator produces 3.9 99 99 99 99 99 99).

[edit] Actually, this is the reason why it calculates more digits than it displays.  The extra digits are "guard" digits to account for rounding errors.
« Last Edit: November 17, 2009, 06:50:06 pm by simplethinker »
"We've all heard that a million monkeys banging on a million typewriters will eventually reproduce the entire works of Shakespeare. Now, thanks to the Internet, we know this is not true." -- Professor Robert Silensky



Chip's Challenge: ħ%

Offline ztrumpet

  • The Rarely Active One
  • CoT Emeritus
  • LV13 Extreme Addict (Next: 9001)
  • *
  • Posts: 5712
  • Rating: +364/-4
  • If you see this, send me a PM. Just for fun.
    • View Profile
Re: Any reliable way to tell if a numbe is an integer in BASIC?
« Reply #6 on: November 17, 2009, 06:42:26 pm »
The fpart glitch isn't limited to just the cube root.  It is caused by the home screen rounding the number when it displays it or by rounding errors due to the calc's floating point nature.

Offline Halifax

  • LV9 Veteran (Next: 1337)
  • *********
  • Posts: 1334
  • Rating: +2/-1
    • View Profile
    • TI-Freakware
Re: Any reliable way to tell if a numbe is an integer in BASIC?
« Reply #7 on: November 18, 2009, 10:58:47 am »
simplethinker: Yeah, binary encoding = BCD, that's what I meant. I was thinking quite possibly that it had to do with a BCD correction error at first, but I was incorrect. Thanks though.

And thanks ztrumpet, Builderboy, and Galandros.
There are 10 types of people in this world-- those that can read binary, and those that can't.