Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Nr has no integer representation

in Questions Posts: 181

I know that rounding to integers ought to fix this error, however the code below still generates this error once in a blue moon and I don’t understand how that’s even possible. Is there any case in which // doesn’t generate an integer?

It happens rarely (but surely) so it’s quite difficult to narrow it down.

local r,g,b,a = map.img:get(lp.x//1, lp.y//1) 
if a/255 >= .1 then ... end


  • dave1707dave1707 Mod
    Posts: 7,468

    @Kirl Since a blue moon happens once every 2.7 years, then you shouldn’t get that error again until Sept 2020. So I wouldn’t worry about it anymore. Just kidding. I wrote a program that took 100,000,000 random numbers between 0.0000000000000 and 99.999999999999 and did the //1 on them without getting the integer error. Without seeing the whole range of code that’s causing the error and being able to run it, I can’t say why it happens. Below I show different numbers with a lot of digits and whether they cause the error or not. What I’m trying to show is that the //1 gave an integer without exception, so something else might be happening. But like I said, without actually having the code I can’t be sure.

    These numbers will cause the integer error
    Those same numbers //1 don’t cause the integer error
    Those same numbers one more digit longer didn’t cause the integer error.
  • dave1707dave1707 Mod
    Posts: 7,468

    @Kirl What type of device are you using and what version of Codea are you running.

  • dave1707dave1707 Mod
    Posts: 7,468

    @Kirl Using the r,g,b,a line of code you show above, these are the 2 errors I can get. The first error I get if I force either lp.x or lp.y to use a fractional number. The second error I get if I force lp.x or lp.y to use a value of 0. Otherwise, I ran your line of code 100,000,000 times with random numbers without getting an error.

    Main:9: bad argument #1 to 'get' (number has no integer representation)
    stack traceback:
        [C]: in method 'get'
        Main:9: in function 'setup'
    Main:9: pixel out of bounds of image, 100, 100 -> given 0, 46
    stack traceback:
        [C]: in method 'get'
        Main:9: in function 'setup'
  • Posts: 181

    I put an extra line in.

    if lp.x//1 %1 ~= 0 or lp.y//1 %1 ~= 0 then print(lp) end Got (nan,nan).
    At least it points to user error as usual. I suppose Im dividing by 0 somwhere...

    Just learned this: print(1/0, 0/0) Prints inf nan
    Reminded me of this:

    Thanks anyway Dave!

Sign In or Register to comment.