NTNUJAVA Virtual Physics Laboratory
Enjoy the fun of physics with simulations!
Backup site http://enjoy.phy.ntnu.edu.tw/ntnujava/
December 18, 2017, 08:21:45 am *
Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
 
   Home   Help Search Login Register  
"It is not the strongest of the species that survive, but the one most responsive to change." ..."Darwin(1809-1882, English naturalist Evolution)"
Google Bookmarks Yahoo My Web MSN Live Netscape Del.icio.us FURL Stumble Upon Delirious Ask FaceBook

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

Posts: 1784


http://weelookang.blogspot.com


WWW
«
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 Offline

Posts: 3080



WWW
«
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 Offline

Posts: 1784


http://weelookang.blogspot.com


WWW
«
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 Offline

Posts: 3080



WWW
«
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
  Print  
"It is not the strongest of the species that survive, but the one most responsive to change." ..."Darwin(1809-1882, English naturalist Evolution)"
 
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 15895 Last post February 24, 2009, 04:45:54 pm
by Fu-Kwun Hwang
Light reflected from arbitrary number of mirrors
Optics
Fu-Kwun Hwang 3 16281 Last post 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 10488 Last post 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 13765 Last post 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 3083 Last post December 19, 2011, 01:38:27 pm
by lookang
Powered by MySQL Powered by PHP Powered by SMF 1.1.13 | SMF © 2006-2011, Simple Machines LLC Valid XHTML 1.0! Valid CSS!
Page created in 0.059 seconds with 23 queries.since 2011/06/15