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 theroid · Jul 29, 2014 at 05:01 PM · loopfloatint

Why does the decimal point matter in my for loop?

I had two identical blocks of code (different variable names), one worked, the other not quite. Straightforward for loops, iterating through an array. The 'z' variable is used in calculations within the loop. This loop worked perfectly:

 for(var z = 0.0; z < zDim; z++)

While this loop iterated through the array, setting the same value every time.

 for(var z = 0; z < zDim; z++)

The only difference is the decimal point. All my other loops seem to work no problem with simple integers. Inside the loops, calculations are made with a mixture of ints and floats. I know my code works, although it would be awesome if somebody could help me understand why.

Comment
Add comment · Show 4
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 robertbu · Jul 29, 2014 at 05:06 PM 0
Share

You might want to post the complete code including how you declared 'zDim'.

avatar image Lazdude17 · Jul 29, 2014 at 05:12 PM 1
Share

Also mixing float and integer calculations can get confusing so I would start there. But indeed you do need to give some more info and code.

avatar image Yofurioso · Jul 29, 2014 at 05:12 PM 0
Share

Can you give a little more information about the code?

avatar image theroid · Jul 29, 2014 at 05:40 PM 0
Share

Here's the block. zDim and xDim are global, declared as int. density, xOrigin and zOrigin are floats, also global. The rest is as it is.

     for(var z = 0.0; z < zDim; z++) {
         for(var x = 0.0; x < xDim; x++) {
             var xCoord = xOrigin + x / xDim * density;
             var zCoord = zOrigin + z / zDim * density;
             var yDim = $$anonymous$$athf.PerlinNoise(xCoord, zCoord);
             vertexArray[index] += Vector3(0, yDim * scale, 0);
             index++;
         }

Does mixing int and float screw things up somewhere?

Apologies for the delay, was battling with my triangle array :)

1 Reply

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

Answer by tanoshimi · Jul 29, 2014 at 05:51 PM

"Does mixing int and float screw things up somewhere" - almost certainly yes, and this is why Javascript's dynamic typecasting is so dangerous. It's pretty unusual for loop iterators to be anything other than ints, so I'd suggest you explicitly declare them as such. i.e.:

 for(var z : int = 0; z < zDim; z++)...

If you want to do some floating point calculations based on the value of the iterator, cast it as a (float) inside the loop block:

 var zCoord : float = zOrigin + (float)z / zDim * density;
Comment
Add comment · Show 3 · 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 theroid · Jul 29, 2014 at 06:09 PM 0
Share

Just tried it as you've typed it here, i get " ';' expected. Insert a semicolon at the end." I'm still a noob, learning all I can. Perhaps I've done something wrong?

It seems like declaring an integer and then casting as a float seems like extra work for the same end. Wouldn't just implicitly declaring z as float be easier? Or would that cause me problems? I assume that the increment would add 1.0 ins$$anonymous$$d of just 1, making float okay for the iterator. Unless that is more taxing (I'm no expert, but I can't imagine it would be much difference).

avatar image tanoshimi · Jul 29, 2014 at 06:49 PM 0
Share

It's not to do with how "taxing" it is - it's the inherent imprecision of floating point maths: 1.0++ may or may not be equal to 2.0....

avatar image theroid · Jul 29, 2014 at 07:51 PM 0
Share

Ah floating point maths gets me again! Thank you for your wise words. You have helped a noob take one more step closer to ... non-noobness.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

How can I move back to Element 0 in an array to reset a loop? 1 Answer

Get the two highest floats in a genericList without using .Sort()? 1 Answer

Unity adding element to 2D array list 2 Answers

Primitive data types (for code, not modeling) 2 Answers

Display a float number when button is clicked 4 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