NaN error

I have been struggling with one of my variables reading NaN and 'infinity'. I have been struggling with it for 2 hours. If I copy and paste the formula for "radius" in my program into wolframalpha, I get a real result. Anyone have any ideas?

void setup() { size(800, 600); background(255); }

float px = 425; float py = 350; float radius=5*atan(5/(1000+800-425))/atan(5/(400+1000));

void star() { fill(#F6FF00); ellipse(400, 300,10,10); }

void planet() {

print(radius); radius = 5*atan(5/(1000+800-px))/atan(5/(400+1000)); ellipse(px,py,5,5); }

void draw() { star(); planet(); }

The variable 'radius' is the problem. It prints the variable as 'NaN' once and then 'Infinity' as it loops through the program.

I took out 90% of the program so it fits in the post. The calculation is superfluous for the written portion on this post but it is important for the rest of the program.

Answers

  • edited August 2016

    In Java the division operator / removes the fractional part from the result if both operands are of whole type. L-)

  • I apologizing for not formatting the text better.

    I replaced the 5/(1000+... with 5./(1000+... in the program and it works great with no errors. Thank you for your time.

    I am struggling why a programming language would drop the fractional/decimal part of a variable just because. Should I just put a decimal place after every integer I use to avoid this? And what do you mean whole type?

    Thanks!

  • edited August 2016
    • Java got 8 primitive datatypes identified by 8 keywords.
    • Apart from boolean, the other 7 are numerical.
    • And outta those 7, 2 are fractional: float & double.
    • All the rest is whole (integer) types: char, byte, short, int, long.
  • edited August 2016

    Should I just put a decimal place after every integer I use to avoid this?

    As I've mentioned it's enough for at least 1 of the operands (the 2 expression sides of the operator) to be fractional. For example: for 1/2 we can 1./2, 1/2. or 1./2.. B-)

  • edited August 2016

    ... why a programming language would drop the fractional/decimal part of a variable just because.

    Consistence & performance. Integer operations are faster than floating 1s after all.
    If we declare that some variable is of type int and we try to assign 4/3 to it, the result would need to be coerced from float or double back to int.
    Java automatically coerces upwards. But refrains for backwards conversions. 8-|

  • Should I just put a decimal place after every integer I use to avoid this?

    I would always add .0 rather than just a . because the latter just looks like punctuation and is easily overlooked

Sign In or Register to comment.