NTNUJAVA Virtual Physics LaboratoryEnjoy the fun of physics with simulations! Backup site http://enjoy.phy.ntnu.edu.tw/ntnujava/
August 21, 2019, 02:13:39 am
 Welcome, Guest. Please login or register.Did you miss your activation email? 1 Hour 1 Day 1 Week 1 Month Forever Login with username, password and session length

 Home Help Search Login Register
"Nothing in life is to be feared, it is only to be understood." ..."Marie Curie 1867-1934, Polish born French Physicist, Twice Nobel Prize Winner- Physics and Chemistry)"

 Pages: [1]   Go Down
 Author Topic: how to make a Ejs way to recognize that a number is 10,100,1000 etc  (Read 6433 times) 0 Members and 1 Guest are viewing this topic. Click to toggle author information(expand message area).
lookang
Hero Member

Offline

Posts: 1792

http://weelookang.blogspot.com

 « Embed this message 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!
 Logged
Fu-Kwun Hwang
Administrator
Hero Member

Offline

Posts: 3082

 « Embed this message Reply #1 on: March 28, 2009, 10:22:38 pm » posted from:Taipei,T\'ai-pei,Taiwan

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.

 Logged
lookang
Hero Member

Offline

Posts: 1792

http://weelookang.blogspot.com

 « Embed this message Reply #2 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)
 Logged
Fu-Kwun Hwang
Administrator
Hero Member

Offline

Posts: 3082

 « Embed this message Reply #3 on: March 30, 2009, 10:37:50 am » posted from:Taipei,T\'ai-pei,Taiwan

What I suggest is 10(log(i2)/log(10))+1.e-8
 Logged
 Pages: [1]   Go Up
"Nothing in life is to be feared, it is only to be understood." ..."Marie Curie 1867-1934, Polish born French Physicist, Twice Nobel Prize Winner- Physics and Chemistry)"
Jump to:

 Related Topics Subject Started by Replies Views Last post how to make a variable number appear in the drawing panel? Questions related to EJS lookang 13 17951 February 24, 2009, 04:45:54 pm by Fu-Kwun Hwang Light reflected from arbitrary number of mirrors Optics Fu-Kwun Hwang 3 17535 October 13, 2010, 12:29:15 pm by Fu-Kwun Hwang is it possible to make applet make a sound when i condition is met? Questions related to EJS lookang 8 11931 December 22, 2008, 12:10:01 pm by lookang Another GUI example: illustrate 10 base/ 2 base and 16 base Number Examples for beginner who want to learn EJS Fu-Kwun Hwang 0 14470 March 29, 2009, 06:17:40 pm by Fu-Kwun Hwang is there a quick way to count the number of Ejs simulations on NTNU Java Virtual Questions related to EJS lookang 0 3640 December 19, 2011, 01:38:27 pm by lookang
Page created in 5.06 seconds with 23 queries.since 2011/06/15