input of long type

edited March 2014 in How To...

I've been doing the project euler challenges and have written a program to determine the prime factors of a given number. To solve the task, I need to input a number whose value exceeds the range of an 'int'.

I've modified my functions to handle a 'long' type, but when I assign the value, it says that the 'literal' still exceeds the range of an 'int', despite declaring it as a type 'long'. The reference section on type 'long' says that processing functions don't use this datatype, so i'm kind of stuck on what to do now.

Any help would be very appreciated.

PS: The site explicitly says not to post solutions to problems anywhere to discourage easy solution finding, so for that reason I didn't include any code. Either way, I think this question doesn't pertain to the actual code, but rather to advice on how to handle 'long' types.


  • edited March 2014 Answer ✓

    Suffix a literal w/ l or L -> 150L

    And when a Processing's function refuse to work w/ long, replace it w/ a corresponding 1 found in class Math:

  • Just to expand on what GoToLoop said:

    When you write a number without a decimal place (1, 2, 9065, -23, etc), Processing (well, Java) assumes that it's an int. That's because int is used more often than long.

    So when you write a number that is outside the range of int (ints have a minimum value of -2^31 and a maximum value of 2^31-1), you get that error. It would be nice if the language automatically converted numbers that were outside of the int range to long, but it doesn't.

    To get around this problem, you can add an L to the end of the number so that the language knows you're specifically talking about a long and not an int. Something like:

    long number = 123456L;

    It's considered a bad practice to use a lower-case L, because it looks too much like a one and can confuse you later!

    A similar thing happens with double and float, as any numbers with a decimal point are assumed to be doubles. But you can convert to float using an F:

    float number = 123.456F;

  • edited March 2014

    Re-expanding the expanded a little more:
    Processing's pre-processor automatically suffixes any number literal that has a . and/or e in it w/ an f. (:|
    If for any reason you'd prefer double literals, suffix them w/ a d or a D. Pre-processor won't touch already suffixed literals!

  • Ah, right, I should have remembered Processing's infatuation with floats! I didn't even realize that D was a suffix, doh!

  • Great answers, guys. Thank you

Sign In or Register to comment.