Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 unity_0sdsWZad6Fwp-A · Apr 14, 2019 at 07:49 PM · arraymathfor-loopincrementpower

Math.Pow and Array Values

Is there a way where I can get the total of the values in an array combined, but the array value is to the a power that is incremented by one for every increase in the array size. Also, if the array value is 0 the value will be skipped example:

 for (int Increment = 0; Increment < ArraySize; Increment++) {
       if(TheArray[Increment] != 0) {
           Math.Pow(TheArray[Increment], Increment);
           } 
 }

Edit: I have realized I have forgotten to mention this, but the number being multiplied by is suppose to be multiplied by 2. The values in my array are dynamic, but say I have 5 values and they are

     0 -- 1 * 2
     1 -- 0 * 2 (skipped (2 * 0 = 0)),
     2 -- 1 * 2,
     3 -- 0 * 2 (skipped (2 * 0 = 0)),
     4 -- 1 * 2,

What I want to happen is for the array row to be the power and the array's value to be then powered by the power and then the values added together.

 2 ^ 0 = 1,
 2 ^ 2 = 4,
 4 ^of 2 = 16, 
 ----------------
 1 + 4 + 16 = 21



Comment
Add comment · Show 2
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 mchts · Apr 14, 2019 at 07:57 PM 0
Share

You mean sum of all values in an array like: [0, 1, 5, 4] => 0 (skipped) + $$anonymous$$ath.Pow(1, 1) + $$anonymous$$ath.Pow(5, 2) + $$anonymous$$ath.Pow(4, 3) = 90 ???

avatar image unity_0sdsWZad6Fwp-A mchts · Apr 14, 2019 at 08:15 PM 0
Share

Yes, but ins$$anonymous$$d of having to write out $$anonymous$$ath.Pow(1,1) + $$anonymous$$ath.Pow(5,2) + $$anonymous$$ath.Pow(4,3) and have it add itself in a for-loop statement.

1 Reply

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

Answer by Bunny83 · Apr 14, 2019 at 09:45 PM

You original code is actually almost complete. All you need is a sum variable

 double sum = 0;
 for (int i= 0; i < TheArray.Length; i++) {
     if(TheArray[i] != 0) {
         sum += Math.Pow(TheArray[i], i);
     } 
 }

You shouldn't use to complicated names for for-loop variables. Especially if they are used for different things. The variable is not an "increment" but just the index into the array.


May I ask what's the actual point of this calculation? It seems a bit strange. I looks a little bit like a generic implementation of the tayler series. Though it would be wrong the way it's handled.

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 unity_0sdsWZad6Fwp-A · Apr 14, 2019 at 10:11 PM 0
Share

I forgot to include that the array value gets multiplied by 2 then to the power. It is suppose to convert binary to integer numbers.

avatar image Bunny83 unity_0sdsWZad6Fwp-A · Apr 14, 2019 at 11:50 PM 1
Share

You want to convert a binary number into an integer? Than using Pow is the most inefficient way of doing so. Just do this:

 int val = 0;
 for (int i= 0; i < TheArray.Length; i++) 
 {
     val |= TheArray[i] << i;
 }

This assumes that the values in the array are either 0 or 1. Of course the values in the array are in reverse order. So the least significant bit is the one at index 0.


Is there a reason you use an array for the individual bits? Usually directly manipulating the bits in a single integer is just as simple as working with an array. To set bit 3 in an integer value, just do

 val |= 1<<3;

To clear a bit just do

 val &= ~(1<<3);

Of course the lease significant bit has the index 0 (just like in your array).

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

126 People are following this question.

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

Related Questions

"For"-Loop only works for final element in array 1 Answer

How to convert and integer into an Array of his digits 4 Answers

Copy values between two classes in two lists. 1 Answer

Error CS0029 Help? (Screenshot of Exact Error) 1 Answer

Using for loop to gather all waypoint objects near player 2 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