Floating point precision problem of Debug.Log function in Unity3d

Source: Internet
Author: User

Transferred from: http://blog.sina.com.cn/s/blog_5d76edd80101iwib.html

You often use the Debug.Log function to print the value of a variable in a program and find that it displays only one decimal place.
This is really too low, like a 0.0xxxxx this decimal is 0.0!!!!!!
The error is too big.
Google, confirming that it is a problem with the Debug.Log function, it is truncated when the vector is turned into a string.
The accuracy can be controlled using the following method:

Debug.Log (VEC. ToString ("F4"))

Reference here: http://answers.unity3d.com/questions/173094/show-vector3-full-float-value.html

When you pass a vector to Debug.Log, I suspect Debug.Log calls the vector ' s ToString ()-method String value out of the argument, the it can write to the console. and the vector3.tostring () is implemented simply rounds the values to 1 decimal.

To get around this issue, you can either pass in the vector ' s coordinates individually, or you can use the overload of ToS Tring that accepts a format, to determine how many decimal points you want the vector represented in. You can use it like this, for example:

Vector3 point = new Vector3 (0.9887f, 1.56789f, 3.09273475f);
Debug.Log (point. ToString ("F4"));

Here, the F means your ' re talking about Fixed-point, and the 4 means you want 4 decimals. You can pass in any number instead of 4. See http://msdn.microsoft.com/en-us/library/dwhawy9k.aspx for further information on what to pass to ToString (string form at).

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.