Shop OBEX P1 Docs P2 Docs Learn Events
Too LONG? — Parallax Forums

Too LONG?

Sniper KingSniper King Posts: 221
edited 2008-09-25 23:29 in Propeller 1
What happens to a local variable (LONG)·if you put a number into it that is too big? If you dimension it as (a[noparse][[/noparse]2]) will that help?

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
·- Ouch, thats not suppose to be hot!··


Michael King
Application Engineer
R&D

Check out Project: PSAV Persistent Sailing Autonomous Vehicle

Comments

  • rokickirokicki Posts: 1,000
    edited 2008-09-25 19:23
    The number has to come from somewhere. On the propeller, the largest supported number type is a 32-bit number,
    and that's how big a LONG is.

    So explain more, please. If I try to store 8_000_000_000 into a LONG, that 8B needs to come from somewhere.

    In general, if the origin is arithmetic (addition, subtraction, multiplication), you will get the result modulo 2^32
    (with the number interpreted with or without a sign).

    But if you give us a bit more context we may be able to provide a more useful answer.
  • Sniper KingSniper King Posts: 221
    edited 2008-09-25 19:42
    Let say I want to save and do math on 80_000_000_000_000 (random)· what happens if I try to store that in a long?

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    ·- Ouch, thats not suppose to be hot!··


    Michael King
    Application Engineer
    R&D

    Check out Project: PSAV Persistent Sailing Autonomous Vehicle
  • Mike GreenMike Green Posts: 23,101
    edited 2008-09-25 19:47
    You can't even do math on numbers greater than 2^32 because all intermediate values are stored in longs. If you multiply 2^30 by 5, the result gets truncated to 2^32. All numbers and all arithmetic operations are performed on 32 bit numbers and the results are 32 bit numbers.

    If you do calculations in floating point, you can have values greater than 2^32, but floating point trades accuracy for range. You may be able to have values much greater than 2^32, but all numbers and all arithmetic operations are limited to about 7 digits of accuracy.

    Post Edited (Mike Green) : 9/25/2008 7:54:01 PM GMT
  • Sniper KingSniper King Posts: 221
    edited 2008-09-25 20:08
    When that happens ie we create a very large number, it wont pour over into the next long will it?

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    ·- Ouch, thats not suppose to be hot!··


    Michael King
    Application Engineer
    R&D

    Check out Project: PSAV Persistent Sailing Autonomous Vehicle
  • TimmooreTimmoore Posts: 1,031
    edited 2008-09-25 20:09
    No it just drops the bits above 32bits, unless you explicitly write code to do that.
  • Sniper KingSniper King Posts: 221
    edited 2008-09-25 20:30
    Ok then, will the Float32Full do math if the value becomes truncated?

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    ·- Ouch, thats not suppose to be hot!··


    Michael King
    Application Engineer
    R&D

    Check out Project: PSAV Persistent Sailing Autonomous Vehicle
  • Mike GreenMike Green Posts: 23,101
    edited 2008-09-25 20:42
    No, not the way you seem to be thinking of it. Look at the Wikipedia article on floating point, particularly IEEE 754 format.
    Google "wiki floating point".

    If you do all your "big" calculations in floating point, the largest number can be 2^127 (roughly 10^38).
    As I mentioned, there are only about 7 significant digits in these values even though the magnitude can be large.
    If you try to subtract 1.0 from 100_000_000.0 using floating point subtraction, you'll get 100_000_000.0 because
    the 1.0 is insignificant. If you try to use integer operations, you'll get garbage.
  • Sniper KingSniper King Posts: 221
    edited 2008-09-25 21:29
    Gotcha... Thanks guys. This really helps. I need to add a check on my numbers to make sure they aren't too big before i start all of my calculations!

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    ·- Ouch, thats not suppose to be hot!··


    Michael King
    Application Engineer
    R&D

    Check out Project: PSAV Persistent Sailing Autonomous Vehicle
  • Paul BakerPaul Baker Posts: 6,351
    edited 2008-09-25 23:29
    Also you have to make sure intermediate values do not overflow either·(you also have to pay attention to whether values are being interpreted as signed or unsigned). Say you have an unsigned ·value := 2^32 - 1000 (shorthand notation to show a large number still representable in 32 bits), if you perform the following math: value * 100 / 1000 you will not receive a valid result, even though the final result can be contained in a 32 bit number. value * 100 is greater than 2^32-1 (largest number representable in a 32 bit number). However if you reconstruct the equation to (value / 1000)*100 you will receive the correct result (truncated to the nearest integer) because the intermediate result value / 1000 is within the range a 32 bit number.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Paul Baker
    Propeller Applications Engineer

    Parallax, Inc.
Sign In or Register to comment.