NTNUJAVA Virtual Physics Laboratory
Enjoy the fun of physics with simulations!
Backup site http://enjoy.phy.ntnu.edu.tw/ntnujava/

Easy Java Simulations (2001- ) => Questions related to EJS => Topic started by: lookang on March 28, 2009, 11:10:47 am



Title: how to make a Ejs way to recognize that a number is 10,100,1000 etc
Post by: lookang on March 28, 2009, 11:10:47 am
how to make a Ejs way to recognize that a number is 10,100,1000 etc

if
number is 1000 to 9999 , how to display 1000 ?

so if a number is 100 to 999, how to display 100 ?

if the number is 10 to 99, how to display 10

number 0 to 9 display as 1 ?

thanks!


Title: Re: how to make a Ejs way to recognize that a number is 10,100,1000 etc
Post by: Fu-Kwun Hwang on March 28, 2009, 10:22:38 pm
Your question is not limited to EJS. It is a general question related to mathematics and programming.

3≦log10(x)<4 if 10002≦log10(x)<3 if 100
However, java did not provide function for log10(x).
It only provide nature log, i.e. ln(x).
log10(x)= loge(x)/loge(10)

and Math.pow(a,b) will return ab

The following formula should give you what you want.
Code:
y=(int)(Math.pow(10,(int)(Math.log(x)/Math.log(10.)+1.e-8)));
1.e-8 was added due to possible numerical error.





Title: Re: how to make a Ejs way to recognize that a number is 10,100,1000 etc
Post by: lookang on March 30, 2009, 10:24:15 am
excellent help.

Code:
scaffold=(int)(Math.pow(10,(int)(Math.log(i2)/Math.log(10.)+1.e-8)));

i assign my own variables ( scaffold and i2) substitution to modify.

thanks for the programming tip.

i read a bit before on Math.pow(a,b) which is ab

so what you suggested is 10(i2/log10 +1.e-8)


Title: Re: how to make a Ejs way to recognize that a number is 10,100,1000 etc
Post by: Fu-Kwun Hwang on March 30, 2009, 10:37:50 am
What I suggest is 10(log(i2)/log(10))+1.e-8