0
    NSLog(@"Number: %f", [self calculateScore]);

    - (CGFloat)calculateScore {
        CGFloat score;
        score = (_bestPossibleMove/_movesMade) * 5.0;
        return score;
    }

So I have 2 instance variables _bestPossibleMove and _movesMade. The former is set to 6.0 from the start, and the latter will be 16.0 by the time the game is completed. When I get to the NSLog message, calling [self calculateScore] keeps giving me 0.0000 when it should be giving me a number slightly smaller than 5.0 and greater than 0.0. Any reason for this?

rmaddy
  • 314,917
  • 42
  • 532
  • 579
Frank
  • 107
  • 9
  • 2
    What are the data types for `_bestPossibleMove` and `_movesMade`? – rmaddy Apr 29 '16 at 20:46
  • Have you put a breakpoint and checked if they are correct before NSLog? – GregP Apr 29 '16 at 20:47
  • @rmaddy is on to something: if the variables are int types the division will either be 0 or 1 (and that only when `_bestPossibleMove == _movesMade`), giving you a score of 0 or 5. – fbrereto Apr 29 '16 at 20:49
  • @fbrereto It's not just 0 or 1. It could be any integer value if both variables are integer types. – rmaddy Apr 29 '16 at 20:52
  • @rmaddy Yep, apologies: I had misinterpreted the code. Regardless, if the variables are both integer types the value will not be what OP expects. – fbrereto Apr 29 '16 at 20:57
  • _bestPossibleMoves and _movesMade are both CGFloats. I have set _bestPossibleMoves set to 6.0 and _movesMade is incremented by int 1 every time I interact with this object that makes the _movesMade increase. – Frank Apr 29 '16 at 21:05
  • Could it be the [self calculateScore] portion? self is (when you track it down) the motion of swiping as the receiver of the message (located in a move method). I could put the NSLog call inside each 4 individual swipeLRDU methods (but I don't think that will make a difference) – Frank Apr 29 '16 at 21:07
  • Also, my apologies, _bestPossibleMove and _movesMade are properties – Frank Apr 29 '16 at 21:17
  • Try changing the `NSLog` to `NSLog(@"%f | %f | Number: %f", _bestPossibleMove, _movesMade, [self calculateScore]);` - or use the debugger. I'm guessing you won't see what you expect... HTH – CRD Apr 29 '16 at 23:02
  • I used _bestPossibleMove as a property. How would I write it so I can set it to 6.0 and be able to retrieve that number? Currently I'm reading 0.0000, everything else though reads as they're supposed to. – Frank Apr 29 '16 at 23:57

2 Answers2

0

Here's answer can help you Float is 0 after integer division

so we just cast one to a float (_bestPossibleMove*1.0)

score = (_bestPossibleMove*1.0/_movesMade) * 5.0;
Community
  • 1
  • 1
lsdoy
  • 129
  • 12
0

when _bestPossibleMove's value is greater than _movesMade's value then /(devision) return 0.so it returns 0.00000.