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
11 42588
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)?
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.
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.
-
-
public class Power {
-
public static void main(String[] args) {
-
int y = 2;
-
double x = Math.pow(2.1, 7);
-
x = Math.pow(y, 7);
-
x = Math.pow(y, 7.7);
-
System.out.println(x);
-
}
-
}
-
This compiles on 1.5
>.>
fine.
What do you mean fine?
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.
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.
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: - public class Tester {
-
public static void main(String[] args) {
-
int n = 2;
-
double x = 2.0;
-
System.out.println(Math.pow(n, x)); // Variable tests...
-
System.out.println(Math.pow(x, n));
-
System.out.println(Math.pow(x, x));
-
System.out.println(Math.pow(n, n));
-
-
System.out.println(Math.pow(n, 3.0)); // Mixed tests (n) ...
-
System.out.println(Math.pow(3.0, n));
-
System.out.println(Math.pow(n, 3));
-
System.out.println(Math.pow(3, n));
-
-
System.out.println(Math.pow(x, 3.0)); // Mixed tests (x) ...
-
System.out.println(Math.pow(3.0, x));
-
System.out.println(Math.pow(x, 3));
-
System.out.println(Math.pow(3, x));
-
-
System.out.println(Math.pow(2, 3)); // Raw Number tests
-
System.out.println(Math.pow(2.0, 3));
-
System.out.println(Math.pow(2, 3.0));
-
System.out.println(Math.pow(2.0, 3.0));
-
}
-
}
This computer has java1.4.something, but I can't figure out how to change the version of Java BlueJ uses.
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: - public class Tester {
-
public static void main(String[] args) {
-
int n = 2;
-
double x = 2.0;
-
System.out.println(Math.pow(n, x)); // Variable tests...
-
System.out.println(Math.pow(x, n));
-
System.out.println(Math.pow(x, x));
-
System.out.println(Math.pow(n, n));
-
-
System.out.println(Math.pow(n, 3.0)); // Mixed tests (n) ...
-
System.out.println(Math.pow(3.0, n));
-
System.out.println(Math.pow(n, 3));
-
System.out.println(Math.pow(3, n));
-
-
System.out.println(Math.pow(x, 3.0)); // Mixed tests (x) ...
-
System.out.println(Math.pow(3.0, x));
-
System.out.println(Math.pow(x, 3));
-
System.out.println(Math.pow(3, x));
-
-
System.out.println(Math.pow(2, 3)); // Raw Number tests
-
System.out.println(Math.pow(2.0, 3));
-
System.out.println(Math.pow(2, 3.0));
-
System.out.println(Math.pow(2.0, 3.0));
-
}
-
}
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
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
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
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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)?...
|
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...
|
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...
|
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...
|
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....
|
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)...
|
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
|
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...
|
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...
|
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
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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,...
|
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...
|
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...
|
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,...
| |