1. #1

    Lua says they don't equal, but they do... wth?

    So I've got some code here:

    Code:
    local HourCodeChanged = false
    local Stored, Current
    for i = 1, 8 do
    	Stored = tonumber(ASDWeekHoursStored[name][i])
    	Current = tonumber(ASDWeekHours[name][i])
    	if Current ~= Stored then
    		HourCodeChanged = true
    		if (printit) then
    			print(name.." iteration: "..tostring(i)..":"..Stored..":"..Current..":")
    			print(type(Stored).." "..type(Current))
    		end
    	end
    end
    if (printit) then
    	print(name.." HourCodeChanged "..tostring(HourCodeChanged))
    end
    return HourCodeChanged
    Now, when printit is true, here's the results:

    Code:
    Solarhoof iteration: 1:9:9:
    number number
    Solarhoof HourCodeChanged true
    For some god forsaken reason, it thinks 9 does not equal 9, and you'd think tonumber() would be a sure fire way to make sure I'm not comparing apples to oranges.

    I'm at a loss here. I've been debugging this one little area for 4 hours now. The first few times this code is called, it's all ok, 9 equals 9. Then every time after that 9 does not equal 9. Reloading UI makes it work again for the first few times. It also doesn't bug out on the other 7 iterations. It's maddening!
    Last edited by JonBoy2001; 2012-09-09 at 02:45 AM.
    Moo.

  2. #2
    Deleted
    Is that an actual, full copy paste of your function? Or did you type it out for the forum/leave out anything?

  3. #3
    This is the else section of an if statement for my function. Copy and pasted. Had to type out the print results though, the :s are there to make sure I didn't have some spaces or something.

    ---------- Post added 2012-09-08 at 09:36 PM ----------

    As a side note, I've tried all sorts of things. Seeing what type my two variables were was the last straw

    ---------- Post added 2012-09-08 at 09:55 PM ----------

    This is on the beta btw, and I have two WoW instances going at the same time for a raid. (this is a modified qdkp2) You don't think they'd be interfering with each other? That wouldn't make sense though, the variables are local and not savedvars....
    Moo.

  4. #4
    Is it possible for a number that is actually 9.000000000001, or some other absurd long number of random 0's before the 1, to show up as a whole number when printed? After coding in some other areas and checking out the savedvars, I noticed some of this behaviour in other variables.
    Moo.

  5. #5
    Deleted
    You may be on to something here. It seems that between 13 and 14 zeros, the print() (and tostring(), too) function ignores them, but the comparison operator does not. As soon as 15 zeros, they both do (and the comparison shows them as equal again).

    Code:
    > local a = {"9."} for i=1,50 do a[i+1] = "0" a[i+2] = 1 local b = tonumber(table.concat(a,"")) print(i,b,tostring(b),b == 9) end
    1       9.01    9.01    false
    2       9.001   9.001   false
    3       9.0001  9.0001  false
    4       9.00001 9.00001 false
    5       9.000001        9.000001        false
    6       9.0000001       9.0000001       false
    7       9.00000001      9.00000001      false
    8       9.000000001     9.000000001     false
    9       9.0000000001    9.0000000001    false
    10      9.00000000001   9.00000000001   false
    11      9.000000000001  9.000000000001  false
    12      9.0000000000001 9.0000000000001 false
    13      9       9       false
    14      9       9       false
    15      9       9       true
    16      9       9       true
    17      9       9       true
    18      9       9       true
    19      9       9       true
    20      9       9       true
    21      9       9       true
    22      9       9       true
    23      9       9       true
    24      9       9       true
    25      9       9       true
    26      9       9       true
    27      9       9       true
    28      9       9       true
    29      9       9       true
    30      9       9       true
    31      9       9       true
    32      9       9       true
    33      9       9       true
    34      9       9       true
    35      9       9       true
    36      9       9       true
    37      9       9       true
    38      9       9       true
    39      9       9       true
    40      9       9       true
    41      9       9       true
    42      9       9       true
    43      9       9       true
    44      9       9       true
    45      9       9       true
    46      9       9       true
    47      9       9       true
    48      9       9       true
    49      9       9       true
    50      9       9       true
    It seems that this behavior occurs for all arithmetic functions - even a multiplication by 100, for example, does not expose it. I'll try to figure out a way to work around this.

    ---------- Post added 2012-09-10 at 03:33 PM ----------

    Upon further examination, this should solve the issue:
    Code:
    Stored = tonumber(tostring(ASDWeekHoursStored[name][i]))
    Current = tonumber(tostring(ASDWeekHours[name][i]))
    Last edited by mmocba105e19de; 2012-09-10 at 01:32 PM.

  6. #6
    Ok, so I'm not crazy, lol. I'll have to sift through QDKP2s code and fix whatever is causing all these extra 0s. The numbers should only be in a ###.# format anyway. Since the comparison operator doesn't ignore it, could it be feasible to have a custom rounding function to round these to the first decimal? Only floor and ceil are available, right? Just in case I can't find the root cause and nip that in the bud, I like fixing root causes than using band-aids.

    Thanks for the help man!
    Moo.

  7. #7
    Deleted
    Code:
    local function round(number,digits)
        return tonumber(("%%.%df"):format(digits):format(number))
    end

  8. #8
    I am not worthy. I forgot about the string.format function.

    I don't think the function name of round would be appropriate though. Maybe Stupid0Fix would suit it better.

    Thanks Treeston!
    Moo.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •