Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Essential · Feb 24, 2012 at 08:43 AM · vector3consolerounding

Getting the same 'rounded' value of a Vector3?

When I write a Vector3 value to the console, the values will be rounded to the nearest decimal (e.g., (0.7, 0.1, 0.0)) but in reality, if I access just that single x value, it's actually something like 0.666879345.

Is the console just multiplying the actual vector by ten, rounding to int, then dividing by ten? Otherwise, I'm curious if there some way to access that already rounded value myself...

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

3 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

Answer by Eric5h5 · Feb 24, 2012 at 09:36 AM

I expect Vector3 just has a function like this:

 public override string ToString () {
     return "(" + x.ToString("f1") + ", " + y.ToString("f1") + ", " + z.ToString("f1") + ")";
 }

That's all, no magic or mystery or already-rounded numbers, sorry. ;)

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image ZweiD · Feb 24, 2012 at 09:41 AM 0
Share

but please don't use the created string to get the rounded float values back ;)

avatar image Eric5h5 · Feb 24, 2012 at 10:04 AM 0
Share

Well, I guess technically that would work though, in a roundabout sort of way. ;)

avatar image
-2

Answer by ZweiD · Feb 24, 2012 at 09:03 AM

that is a general problem in computing (see Wikipedia on Floating Point Values). the computer cannot 'really' display a 0.7, instead he saves that the number as something like 0.666879345.

Therefore I don't think what you want is possible and a problem of the represantation of floating values in general.

Comment
Add comment · Show 10 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image ZweiD · Feb 24, 2012 at 09:10 AM 0
Share

you can also see this effect in the unity editor when editing the position of an object, closing unity and reopening it. often the value of the position vector of the object will have changed slightly.

if you really need to test against the first decimal place, the best thing to do would probably be to multiply by ten, round and cast the result to int (also save it as an int, otherwise you have the same problem that you had).

if you only compare these values to each other, you don't actually have a problem because they were all multiplied by 10.

avatar image Essential · Feb 24, 2012 at 09:12 AM 0
Share

Interesting link… Although I think I might have explained poorly what I was asking. I don't want the full float, I just want to use the same number being displayed in the console.

 var figure = Vector3(0, 0.599999, 0);
 
 Debug.Log(figure);

would show as 0.0, 0.6, 0.0 in the console.

But if I try…

 if (figure.y == 0.6)
   Debug.Log("hello!");

...It won't work. :)

avatar image Essential · Feb 24, 2012 at 09:13 AM 0
Share

I guess it's not possible… Was just curious though. Thanks.

avatar image ZweiD · Feb 24, 2012 at 09:16 AM 1
Share

like I (and you started) said: you can do:

if (((int) figure.y * 10) == 6) Debug.Log("i'm at 0.6");

avatar image Eric5h5 · Feb 24, 2012 at 09:41 AM 0
Share

There are no issues using .7 in floating point. If a number can't be represented exactly by floating point, it will be off by a very small amount, like .0000001. The issue here isn't floating point or inaccuracies thereof, it's just a matter of the Vector3 ToString function deliberately rounding numbers so that printing a Vector3 doesn't result in long strings of precision that you (probably) don't really care about. (In those cases where you do, it's mildly annoying, but can be bypassed by printing the individual elements. If you need to do that a lot, it's worth making a small function that prints the elements "as-is" and doesn't do any rounding.)

Show more comments
avatar image
0

Answer by senad · Feb 24, 2012 at 09:28 AM

You could use an epsilon value to check if your float is inside a given range. For example:


float espilon = .05f;
if ( Math.abs(figure.y - .7f) < epsilon ) figure.y = .7f;
Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image ZweiD · Feb 24, 2012 at 09:33 AM 0
Share

be aware that this will not work reliably, though it will probably cover about 99% of all cases ...

(meaning: it is ok to do it in unity for a small project but not for anything bigger or for a system that has to be highly reliable)

avatar image senad · Feb 24, 2012 at 09:44 AM 0
Share

I do not understand, why is it not reliable? It will always cover the range of all float values in the range of ].65f, .75f[ in this case. (not including the numbers .65 and .75 themselves.

avatar image ZweiD · Feb 24, 2012 at 09:55 AM 0
Share

this is reliable for the simple case that is shown here, but it fails for example if you are multiplying the value beforehand. because then the discrepancy between the real number and the rounded one gets bigger.

avatar image senad · Feb 24, 2012 at 10:10 AM 0
Share

As i understand the question he is looking for a way to round the float without converting it to int prior.

So I do not think your point is valid as this is a valid method for the rounding. Perfor$$anonymous$$g calculations with floating point numbers is a different issue. :)

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

8 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Vector3.ToString 3 Answers

Rounding a position against the surface of an object. 1 Answer

How precise is unity with it´s transforms? 2 Answers

Show Vector3 full float value in Debug 1 Answer

Convert directions in Vector3 to yaw/pitch/(roll),Converting directions in Vector3d to yaw/pitch/(roll) 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges