0

My code is

Double x= 400.002015366677,y =300.000014;
Point point = new Point(x,y);

In this above point coordinate value is setting properly in the WPF but in the UWP the values are changed.Its value is 400.0020141601525 and 300.

how to get the original value of point in uwp?

thanks,,

Genish Parvadia
  • 1,437
  • 3
  • 17
  • 30
Santhiya
  • 191
  • 2
  • 14
  • 1
    `double` is not a precise value type. You are going to see truncation errors, and while the program will do its best to compensate, it is not perfect. See http://stackoverflow.com/questions/753948/why-is-floating-point-arithmetic-in-c-sharp-imprecise – Abion47 Dec 20 '16 at 11:51
  • This may be due to the inherent lack of accuracy in floating point numbers. https://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html – JamesFaix Dec 20 '16 at 11:52
  • @JamesFaix Floating point value does not "lack accuracy". "accuracy" is not a boolean attribute, in other words, a variable/value cannot be said to either *be* accurate or *not be* accuract. Instead, accuracy is a relative term, a variable/value can be said to be accurate to a certain number of digits. – Lasse V. Karlsen Dec 20 '16 at 11:57
  • I wasn't suggesting "accuracy" is boolean, just that floating point types are inherently less accurate than integers or fixed-precision decimals. – JamesFaix Dec 20 '16 at 12:00
  • Use decimal or BigInteger and handle decimal points yourself – FCin Dec 20 '16 at 12:40
  • All these comments about precision/accuracy do not explain the observed behaviour of the UWP Point structure. Both numbers fit well into a double precision floating point value (with 15 significant digits). If you create your own Point structure with two double properties initialized from two constructor parameters, you can read back exactly the values passed to the constructor. – Clemens Dec 20 '16 at 13:22
  • 1
    We may assume that Point internally stores the numbers a single precision values, which would explain truncation of `300.000014` to `300`. However that does not explain why `400.002015366677` is converted to `400.0020141601525`. – Clemens Dec 20 '16 at 13:30

1 Answers1

0

Unlike the Point Structure used in WPF, Point structure in UWP is a Windows Runtime structure that under Windows.Foundation namespace and can be used across .NET, C++ and JavaScript.

If we look at Fields of Point structure, we will find the fields use a data type as following:

Number [JavaScript] | System.Single [.NET] | float32 [C++]

So internally, the value is stored as single-precision 32-bit number. However, while using Microsoft .NET language, Point's data members are exposed as double properties, not fields.

Ref Projection and members of Point section of Remarks:

If you are using a Microsoft .NET language (C# or Microsoft Visual Basic), or Visual C++ component extensions (C++/CX), then Point has non-data members available, and its data members are exposed as read-write properties, not fields. .NET supports this functionality through the System.Runtime.WindowsRuntime.dll interop assembly that's shipped as part of .NET for Windows Runtime apps.

So while using C# or Visual Basic, we use the Point.Point constructor that accepts two System.Double parameters. For C# or Visual Basic, the data type of Point.X and Point.Y property is also System.Double.

And what happens in your code is that while initializing, your x and y values are first converted to System.Single and when retrieving property, it converts to System.Double again like:

Double x = 400.002015366677, y = 300.000014;

Double X = Convert.ToDouble(Convert.ToSingle(x));  //X is 400.00201416015625

Double Y = Convert.ToDouble(Convert.ToSingle(y));  //Y is 300

For Point structure, I'm afraid we can't get the accurate original value. Point is usually used for UI positioning and rendering. In this scenario, it is recommended to use integer values.

X and Y for a Point can be non-integer values. However, this can introduce the possibility of sub-pixel rendering, which can change colors and anti-alias any straight line along the sub-pixel edge. That's how the XAML rendering engine treats sub-pixel boundaries. So for best results, use integer values when declaring coordinates and shapes that are used for UI positioning and rendering.

If you do need double values, I'd suggest you define a new structure and do not use Point.

Jay Zuo
  • 15,653
  • 2
  • 25
  • 49