0

I'm getting a runtime error in my Swift code:

EXC_BAD_INSTRUCTION (code=EXC_i386_INVOP, subcode=0x0)

At line:

y = height - (((val - minYval) / (maxYval - minYval)) * height)

In context:

    let height:CGFloat = self.frame.height - yOffset
    let width = self.frame.width - xOffset - 50

    let num = CGFloat(keys.count)

    for var i = 0; i < keys.count; ++i {

        var y:CGFloat = (height/CGFloat(2.0))

        var val = CGFloat(values[i])

        println("height=\(height), val=\(val), minYval=\(minYval), maxYval=\(maxYval), y=\(y)")

        if CGFloat(maxYval - minYval) != CGFloat(0) {
            println("debug 1")
            y = height - (((val - minYval) / (maxYval - minYval)) * height)
        } else {
            println("x=\(x), y=\(y)")
        }

    }

Output:

height=193.5, val=355.0, minYval=355.0, maxYval=355.0, y=96.75

x=45.0, y=96.75

Update: added values for minYval & maxYval.

Update: I have < 10 reputation, so no images.

olympia
  • 362
  • 2
  • 20
  • 1
    I would suggest you print out maxYval and minYval prior to your if() test, run your code, generate your crash, and if you still need help, add the results of that test to the question. – Brad Brighton Mar 10 '15 at 21:29
  • @BradBrighton I updated it, still getting the same crash. – olympia Mar 10 '15 at 22:40
  • I don't doubt you'll get the crash. The printout of the values in question should have given you a clue as to why (or might help others do the same). What you posted is interesting, but not the actual runtime values of those variables. In fact, why not print out every variable in the equation - height, val, minYval, maxYval. Take a screenshot of that from the log so we can see _actual_ values. – Brad Brighton Mar 10 '15 at 22:44
  • Have you checked `keys.count` is guaranteed to match `values.count`? Otherwise you may be getting an out-of-bounds error in fetching `values[i]`. In fact you seem to be iterating over `keys.count` but then never indexing into `keys`. Are you sure you can’t just do `for value in values { }` to achieve the result you want? – Airspeed Velocity Mar 10 '15 at 23:04
  • @BradBrighton posted. odd thing is, maxYval and minYval are the same (debug prints show this) but the crash superficially appears on the line mentioned. maybe the crash is happening elsewhere and manifesting itself incorrectly on this line? – olympia Mar 10 '15 at 23:14
  • Thanks for making the updates... You do realize that when you manually plug in the numbers you printed, you're getting (355.0 - 355.0) * 193.5, for a divide-by-zero, so your test to prevent it is failing. – Brad Brighton Mar 10 '15 at 23:18
  • @BradBrighton My test to prevent divide-by-zero is `if 355-355 != 0` given what I've printed. Also, as the debug output doesn't print "debug 1", I don't think it should even hit the line where it fails. – olympia Mar 11 '15 at 02:31
  • First, you're right, I read your output too quickly. Second, the only other suggestion would be to get the stack trace and post that. Make sure you're breaking on exceptions, use the debugger to step through to guarantee where and when you're actually crashing, that sort of thing, and look at what @AirspeedVelocity said earlier in the thread for more possible clues. – Brad Brighton Mar 11 '15 at 02:35

1 Answers1

0

RESOLUTION:

I commented out sections until my program completed successfully. After narrowing down the error (happening many, many lines later) I seem to have been doing a bad modulo (%) operation.

Hope this helps.

olympia
  • 362
  • 2
  • 20