Win a copy of Testing JavaScript Applications this week in the HTML Pages with CSS and JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

math. class in java

 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
331(t+273) ^1/2
_____
( 273 )

331 multiply t+273/273 the result is exponent 1/2
how do you write this equation in java using math.class


thanks
 
Rancher
Posts: 43016
76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Exponent 0.5 means square root - there's a method for that in the Math class.
 
Juan Villena
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


So you can express it like this:
331.5*(math.sqrt((t8273)/273)

is this right???


 
Juan Villena
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

331.5* (math.sqrt((t*273)/273))
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Juan Villena wrote:331.5*(math.sqrt((t8273)/273)


That doesn't look correct. Why suddenly 331.5 instead of 331?

From your description it is unclear of what exactly you want to take the square root. Just of the (t + 273) part or of the whole equation?
 
Sheriff
Posts: 21972
106
Eclipse IDE Spring VI Editor Chrome Java Ubuntu Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There's also a method Math.pow which takes two doubles. You could use that if the exponent is a fraction other than 0.5.
 
Marshal
Posts: 69874
278
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Rob Spoor wrote:There's also a method Math.pow which takes two doubles. You could use that if the exponent is a fraction other than 0.5.

. . . or 0.3333333333333333333333333333333333333333333 because there is a cube root method too.
Don't use Math,pow(x, 2) for squares. Use x * x instead.
 
Juan Villena
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jesper the jonq, actually , i want to square root the whole equation, an\d is 331

Rob Spoor and Campbell Ritchie: so can i use math.pow in this particular ecuation or just math.sqrt
and can you tell if this is right or wrong : 331*(math.sqrt((t*273)/273)
or can you guys write this ecuation using math class please.

thanks a lot
 
lowercase baba
Posts: 12871
62
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
<pedant>
There is no such thing as a 'math' class in java. There is, however, a Math class.
</pedant>
 
Campbell Ritchie
Marshal
Posts: 69874
278
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As Fred has pointed out, capital M.

... Math.sqrt(123.456) ...? Yes.
 
Jesper de Jong
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you want the square root of the whole equation, then you should obviously put the whole equation between the brackets for the method call to Math.sqrt:

So, Math.sqrt(331 * ...) and not 331 * Math.sqrt(...)
 
Ulf Dittmer
Rancher
Posts: 43016
76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is the difference between "t" and "(t*273)/273" ? Is that intentional?
 
Campbell Ritchie
Marshal
Posts: 69874
278
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think Jesper has seen the error and inadvertently corrected it from * to +. With 273 in, probably something to do with absolute temperatures.

Beware of integer arithmetic: for any t such that t < 273 and t ≥ 0, the result of the division will turn out to be 1, if t is an integer.
 
Juan Villena
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot, I understand now...about Math.class... But my doubt is about the exponent ' 1/2 '. Using the same equation, if I want to elevated it to exponent 1/2, what Math.class should I use n which one is the correct one.
Math.sqrt or Math.pow ??

Thanks
 
fred rosenberger
lowercase baba
Posts: 12871
62
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Did you read the API on the Math class? It tells you quite clearly:

static double sqrt(double a)
Returns the correctly rounded positive square root of a double value.


so if you want the square root, you use this.

static double pow(double a, double b)
Returns the value of the first argument raised to the power of the second argument.


If you want to raise a number to some arbitrary power, you use this.

Since raising a number to the 1/2 power is BY DEFINITION taking the square root, you can use either one you want.
 
Campbell Ritchie
Marshal
Posts: 69874
278
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually if you give the exponent 1 / 2, you will receive the result 1.0.

The reason is that 1 / 2 is integer arithmetic, and will return 0, so you will get x to the power of 0, and that returns 1.

I would say, if they have got a method which is specially designed for square roots, use that method. It's easier for a start.
 
    Bookmark Topic Watch Topic
  • New Topic