2

I'm trying to use manipulation to rescale a canvas. On debugging the below code it seems that canImage.Width and canImage.Heigh both get set to NaN. I don't understand how a double times a double can give Nan

(width ~ 400 Height ~400 e.scale.y ~-1.5 e.Scale.X ~0.3)

.

    private void viewer_ManipulationCompleted(object sender, System.Windows.Input.ManipulationCompletedEventArgs e)
    {
        if (e.TotalManipulation.Scale.X != 0 && e.TotalManipulation.Scale.Y != 0) {
            canImage.Width = mainImage.Width * (double)e.TotalManipulation.Scale.X;
            canImage.Height = mainImage.Height * (double)e.TotalManipulation.Scale.Y;
        }
    }

EDIT: Just put a conditional breakpoint in and it seems e.TotalManipulation.Scale.X and e.TotalManipulation.Scale.X are never NaN. Putting the e.TotalManipulation.Scale.X > 0 condition did stop the issue. It looks like setting Height/Width to something less than one just causes them to become NaN rather than just falling over. Thanks for all your help

Tom Squires
  • 8,848
  • 12
  • 46
  • 72
  • 4
    Have you confirmed neither `mainImage.Width` nor `e.TotalManipulation.Scale.X` are `NaN`? – Greg Aug 02 '11 at 21:25
  • 1
    Had this issue and the Manipulation.Scale.X values were NaN. There's great code here from when I asked this question, see the answer: http://stackoverflow.com/questions/5507416/constraints-on-image-width-height-on-wp7-gesturelistener – William Melani Aug 02 '11 at 21:26
  • Why the `(double)` typecasts? – H H Aug 02 '11 at 21:40
  • Because I thought multiplying was the issue so I wanted to be sure it was the correct type. Just clutching at straws really. – Tom Squires Aug 02 '11 at 21:45

2 Answers2

2

Is that negative sign supposed to be there for e.scale.y = -1.5? Setting the height to a negative number may explain your problems.

Egor
  • 1,622
  • 12
  • 26
1

If e.TotalManipulation.Scale.X or e.TotalManipulation.Scale.Y are equal to NaN, then the product will be equal to NaN. Try testing using the following instead (since you wouldn't want negative numbers either):

if (e.TotalManipulation.Scale.X > 0 && e.TotalManipulation.Scale.Y > 0)
Greg
  • 23,155
  • 11
  • 57
  • 79
  • 1
    @Greg That's not how you check for NaN. You should either compare a number to itself (by definition NaNs are not equal to any number, including another NaN) or use `Double.IsNaN` – Praetorian Aug 02 '11 at 21:32
  • @Praetorian - It **does** work. I just tested it: `(Double.NaN > 0)` returns false. This is a fine approach because the OP should also reject negative numbers. – Greg Aug 02 '11 at 21:34
  • 1
    You should add a comment though. That this code is intended to cope with NaN and rejects them. – CodesInChaos Aug 02 '11 at 21:40
  • @Greg It may work, but if you read the [IEEE 754 spec](https://secure.wikimedia.org/wikipedia/en/wiki/NaN) on NaN, the equality test is the only one guaranteed to not throw an exception. Other comparisons will throw an exception if the NaN happens to be a signalling NaN. – Praetorian Aug 02 '11 at 22:14