1

I was just going though some fairly simple code and came across the following Math to translate degrees to radians,

 degrees = customSettingsObj.percent * 360.0;
 radians = degrees * (Math.PI / 180);

Now customSettingsObj.percent can be a percentage value between 0% and 100% , in my case its 75.

So in my case the math evaluates to the following:

 degrees = 75 * 360.0;  // evalues to 27000
 radians = degrees * (Math.PI / 180);   evalues to 471.23889803846896

I don't understand the math going on above. can anybody explain ?

Thank you.

  • 1
    May I guess that customSettingObj.percent is supposed to be between 0 and 1 instead of 0 and 100? – Tunococ Jan 09 '16 at 15:30
  • @Tunococ thanks for your response , but its between 0 and 100 :) not between 0 and 1 . – Alexander Solonik Jan 09 '16 at 15:32
  • If the author of the code has half a sense, the percentage is a fraction between 0 and 1, the 0-100 range is never useful except for human intuition. – orion Jan 09 '16 at 15:32

1 Answers1

1

Presumably your customsettingObj.percent is supposed to represent the percent of a circle the angle represents. As it is a percent, you should divide by $100$, and indeed $75\%$ of a full circle is $270^\circ$. The conversion to radians is fine.

Ross Millikan
  • 374,822
  • Presumably your customsettingObj.percent is supposed to represent the percent of a circle Yup :D – Alexander Solonik Jan 09 '16 at 15:32
  • how is it fine ? the author is not dividing anything by 100 ? can you emphasis a bit more ? – Alexander Solonik Jan 09 '16 at 15:37
  • @AlexanderSolonik: maybe the author is storing the percent as a value between $0$ and $1$. That should not be referred to as a percentage, but sometimes people do. Certainly if you intend $75$ to represent $3/4$ of a circle, you need to divide by $100$. – Ross Millikan Jan 09 '16 at 15:42
  • I beleive your right and as of now degrees = 75 * 360.0; // evalues to 27000 radians = degrees * (Math.PI / 180); evalues to 471.23889803846896 just does't make sense (the formula for degrees to radians is correct ... but customSettingsObj.percent * 360.0 is wroung i believe , unless we devide it by 100 ) – Alexander Solonik Jan 09 '16 at 15:49
  • The code is not right or wrong until the variables are defined. You are assuming from the variable name that customsettingObj.percent should range from $0$ for nothing to $100$ for a full circle. It is a reasonable assumption, but may not match the author's definition. The author may be using $1$ to represent a full circle. In that case the percent in the name is misleading, but as long as the code is consistent it will give correct results. It does make it harder on other people trying to understand the code. – Ross Millikan Jan 09 '16 at 16:02
  • i was't assuming , i've checked the value in my dev tools , the code works fine , not surprisingly , that calculated value , is not used at all ... maybe the author forgot to take it off or was just experimenting , either way , i know its the wrong value now :) thanks . – Alexander Solonik Jan 09 '16 at 16:10