473,396 Members | 2,002 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Power function in Java

hi guys,

i want to find 2^n (pow(2,n)).

I am doing java.lang.Math and using power function. But error occured. Because my variable is integer type. but the function arguments are double.

Can u give me a solution to find 2^n using java
Dec 11 '06 #1
11 42588
r035198x
13,262 8TB
hi guys,

i want to find 2^n (pow(2,n)).

I am doing java.lang.Math and using power function. But error occured. Because my variable is integer type. but the function arguments are double.

Can u give me a solution to find 2^n using java
Why not change your argument to double then with pow(2.0, n)?
Dec 11 '06 #2
Ganon11
3,652 Expert 2GB
Exactly what I was going to suggest. Math.pow() either takes two integers or two doubles, but not a mix of the two, for some reason.
Dec 11 '06 #3
r035198x
13,262 8TB
Exactly what I was going to suggest. Math.pow() either takes two integers or two doubles, but not a mix of the two, for some reason.
Expand|Select|Wrap|Line Numbers
  1.  
  2. public class Power {
  3.  public static void main(String[] args) {
  4.   int y = 2;
  5.   double x = Math.pow(2.1, 7);
  6.   x = Math.pow(y, 7);
  7.   x = Math.pow(y, 7.7);
  8.   System.out.println(x);
  9.  }
  10. }
  11.  
This compiles on 1.5
Dec 11 '06 #4
Ganon11
3,652 Expert 2GB
>.>

fine.
Dec 11 '06 #5
r035198x
13,262 8TB
>.>

fine.
What do you mean fine?
Dec 11 '06 #6
Ganon11
3,652 Expert 2GB
Well, I was just being slightly sarcastic. Whenever I had used Java, it didn't let me use an int and a double - I was getting the same error as anibio, and that's why I replied the way I did. Then you basically said I was wrong according to Java 1.5. So I was 'fake' frustrated - no big deal though.
Dec 11 '06 #7
r035198x
13,262 8TB
Well, I was just being slightly sarcastic. Whenever I had used Java, it didn't let me use an int and a double - I was getting the same error as anibio, and that's why I replied the way I did. Then you basically said I was wrong according to Java 1.5. So I was 'fake' frustrated - no big deal though.
Actually I'm not fully satisfied yet. I haven't tried it on a 1.4 compiler yet so I'm not sure about the behaviour on 1.4 yet since 1.5 has this autoboxing thing added to it which may have helped. The docs have Math.pow(double x, double y) both for 1.4 and 1.5 so I don't think the behaviour will be different.

I was afraid you'd got too frustrated with my reply.
Dec 11 '06 #8
Ganon11
3,652 Expert 2GB
Hmm...using java1.5.0_04 (through BlueJ) I get the results:
9.0
8.0
8.0
8.0
8.0
4.0
4.0
4.0
4.0
8.0
9.0
8.0
9.0
8.0
9.0
8.0
9.0
8.0
8.0
8.0
8.0

from the program:

Expand|Select|Wrap|Line Numbers
  1. public class Tester {
  2.     public static void main(String[] args) {
  3.         int n = 2;
  4.         double x = 2.0;
  5.         System.out.println(Math.pow(n, x)); // Variable tests...
  6.         System.out.println(Math.pow(x, n));
  7.         System.out.println(Math.pow(x, x));
  8.         System.out.println(Math.pow(n, n));
  9.  
  10.         System.out.println(Math.pow(n, 3.0)); // Mixed tests (n) ...
  11.         System.out.println(Math.pow(3.0, n));
  12.         System.out.println(Math.pow(n, 3));
  13.         System.out.println(Math.pow(3, n));
  14.  
  15.         System.out.println(Math.pow(x, 3.0)); // Mixed tests (x) ...
  16.         System.out.println(Math.pow(3.0, x));
  17.         System.out.println(Math.pow(x, 3));
  18.         System.out.println(Math.pow(3, x));
  19.  
  20.         System.out.println(Math.pow(2, 3)); // Raw Number tests
  21.         System.out.println(Math.pow(2.0, 3));
  22.         System.out.println(Math.pow(2, 3.0));
  23.         System.out.println(Math.pow(2.0, 3.0));
  24.     }
  25. }
This computer has java1.4.something, but I can't figure out how to change the version of Java BlueJ uses.
Dec 11 '06 #9
r035198x
13,262 8TB
Hmm...using java1.5.0_04 (through BlueJ) I get the results:
9.0
8.0
8.0
8.0
8.0
4.0
4.0
4.0
4.0
8.0
9.0
8.0
9.0
8.0
9.0
8.0
9.0
8.0
8.0
8.0
8.0

from the program:

Expand|Select|Wrap|Line Numbers
  1. public class Tester {
  2. public static void main(String[] args) {
  3. int n = 2;
  4. double x = 2.0;
  5. System.out.println(Math.pow(n, x)); // Variable tests...
  6. System.out.println(Math.pow(x, n));
  7. System.out.println(Math.pow(x, x));
  8. System.out.println(Math.pow(n, n));
  9.  
  10. System.out.println(Math.pow(n, 3.0)); // Mixed tests (n) ...
  11. System.out.println(Math.pow(3.0, n));
  12. System.out.println(Math.pow(n, 3));
  13. System.out.println(Math.pow(3, n));
  14.  
  15. System.out.println(Math.pow(x, 3.0)); // Mixed tests (x) ...
  16. System.out.println(Math.pow(3.0, x));
  17. System.out.println(Math.pow(x, 3));
  18. System.out.println(Math.pow(3, x));
  19.  
  20. System.out.println(Math.pow(2, 3)); // Raw Number tests
  21. System.out.println(Math.pow(2.0, 3));
  22. System.out.println(Math.pow(2, 3.0));
  23. System.out.println(Math.pow(2.0, 3.0));
  24. }
  25. }
This computer has java1.4.something, but I can't figure out how to change the version of Java BlueJ uses.

I got

4.0
4.0
4.0
4.0
8.0
9.0
8.0
9.0
8.0
9.0
8.0
9.0
8.0
8.0
8.0
8.0

On 1.5_08
Dec 11 '06 #10
Ganon11
3,652 Expert 2GB
Whoops, on that last post you can ignore the first 5 values. They somehow got copied onto my paste clipboard when copying from the results.

I got these results at home using java1.4.1_07
4.0
4.0
4.0
4.0
8.0
9.0
8.0
9.0
8.0
9.0
8.0
9.0
8.0
8.0
8.0
8.0
Dec 11 '06 #11
r035198x
13,262 8TB
Whoops, on that last post you can ignore the first 5 values. They somehow got copied onto my paste clipboard when copying from the results.

I got these results at home using java1.4.1_07
4.0
4.0
4.0
4.0
8.0
9.0
8.0
9.0
8.0
9.0
8.0
9.0
8.0
8.0
8.0
8.0
Therefore no surprises as expected. I wonder what problem the OP was getting on this one
Dec 12 '06 #12

Sign in to post your reply or Sign up for a free account.

Similar topics

21
by: utab | last post by:
Hi there, Is there a way to convert a double value to a string. I know that there is fcvt() but I think this function is not a part of the standard library. I want sth from the standard if...
11
by: Russ | last post by:
I have a couple of questions for the number crunchers out there: Does "pow(x,2)" simply square x, or does it first compute logarithms (as would be necessary if the exponent were not an integer)?...
32
by: chris.fairles | last post by:
Just want an opinion. I have an algorithm that needs to run as fast as possible, in fact. Its already too slow. I've done as much algorithmic changes as I can to reduce the amount of code, so now...
1
by: marty.gagnon | last post by:
I have a xsl file that references a xml file using the document() function. I'm having trouble specifying the URI in the document() function of a xls stylesheet. I'm using java to transform the...
5
by: Gus007 | last post by:
Hi all, Need the community great support once more. :) I need to know how to calculate the power of some numbers in C, the problem is that the number is too big , and the compiler gives a...
5
by: elsa | last post by:
hi i need help in writing a function integerPower(base,exponent) that returns the value base^exponent with assuming that the exponent is a positive, nonzero integer and that the base is an integer....
40
by: logicode | last post by:
Hi, I usualy try to solve class problems on my own but I am hitting a wall. I am supposed to write a power function and call it in main but having problems like void power(int base,int exponent)...
11
by: Chris Forone | last post by:
hello group, is there some reference implementation of a fast power function? how fast is the power function in <cmath>? thanks & hand, chris
9
by: suppamax | last post by:
Hi everybody! I'm writing a C program for a PIC18F microcontroller. I need to calculate a power function, in which both base and exponent are fixed point numbers (ex: 3.15^1.13). Using...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.