This site requires JavaScript, please enable it in your browser!
Greenfoot back
Nosson1459
Nosson1459 wrote ...

2017/3/1

Exponent

Nosson1459 Nosson1459

2017/3/1

#
How does this line of code do 'a' to the exponent of 'b'?
public static native double pow(double a, double b);
The above code is in the StrictMath class (It's the whole method). In the Java Math class is (also the full method):
public static double pow(double a, double b) {
    return StrictMath.pow(a, b); // default impl. delegates to StrictMath
}
With this code anyone is able to do:
double power = Math.pow(2, 3);
and then the variable 'power' will be equal to 8.0. How? I don't really know what a native method is so that could be the reason that I don't understand whatever it is that I'm missing. Because when I try:
/**
 * Write a description of class TestMath here.
 * 
 * @author Yehuda (1/2 of Nosson1459 - greenfoot.org user name)
 * @version 2/28/2017
 */
public final class TestMath {
    
    private TestMath() {}

    public static double pow(double a, double b) {
        return TestStrictMath.pow(a, b); // default impl. delegates to TestStrictMath
    }
}
With:
/**
 * Write a description of class TestStrictMath here.
 * 
 * @author Yehuda (1/2 of Nosson1459 - greenfoot.org user name)
 * @version 2/28/2017
 */
public final class TestStrictMath {

    private TestStrictMath() {}

    public static native double pow(double a, double b);
}
I get an error when it runs through the code using the method saying:
java.lang.UnsatisfiedLinkError: TestStrictMath.pow(DD)D
	at TestStrictMath.pow(Native Method)
	at TestMath.pow(TestMath.java:12)
	at MyWorld.<init>(MyWorld.java:81)
	at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
	at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
	at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
	at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
	at greenfoot.core.Simulation.newInstance(Simulation.java:617)
	at greenfoot.platforms.ide.WorldHandlerDelegateIDE$3.run(WorldHandlerDelegateIDE.java:425)
	at greenfoot.core.Simulation.runQueuedTasks(Simulation.java:502)
	at greenfoot.core.Simulation.maybePause(Simulation.java:305)
	at greenfoot.core.Simulation.runContent(Simulation.java:218)
	at greenfoot.core.Simulation.run(Simulation.java:211)
in MyWorld on line 81 I have:
showText("" + TestMath.pow(2, 3), 50, 50);
Nosson1459 Nosson1459

2017/3/2

#
No one seems to know the answer to this question.
davmac davmac

2017/3/2

#
'native' means that there is a native implementation of the method, not written in Java, but in (for example) C or C++ and included in a library which has been loaded (via for example System.loadLibrary()) or, in this case, included in the Java VM itself. The native source code for the StrictMath class can be found here.
davmac davmac

2017/3/2

#
(Note that the native method just calls "jpow", which must be defined somewhere else, but which probably just calls out to the C library's "pow" function).
Nosson1459 Nosson1459

2017/3/3

#
Thanks. I don't know if I fully understand but I think what you're saying is that it's a line of code which is calling a method (or creating) from elsewhere which already exists.
davmac davmac

2017/3/3

#
Well, it doesn't call the method. It just declares that the method exists, and that its implementation is elsewhere. The line of code that calls the method is this one:
    return StrictMath.pow(a, b); // default impl. delegates to StrictMath
Nosson1459 Nosson1459

2017/3/3

#
That line is calling the method in StrictMath class from Math class which I understand, but in StrictMath class is a method that's just one line and you spoke about it with your other posts I would think.
davmac davmac

2017/3/3

#
but in StrictMath class is a method that's just one line and you spoke about it with your other posts I would think
The method in StrictMath with one line is the one I linked to, which is written in C:
JNIEXPORT jdouble JNICALL
Java_java_lang_StrictMath_pow(JNIEnv *env, jclass unused, jdouble d1, jdouble d2)
{
    return (jdouble) jpow((double)d1, (double)d2);  // is this the one line you mean?
}
But I feel like you are talking about this:
public static native double pow(double a, double b);
... which is just a method declaration; It's not correct to say that it is a method with one line of code. On the other hand, maybe I've misunderstood you.
Nosson1459 Nosson1459

2017/3/5

#
I think you understood me. I was talking about the line of code in StrictMath. I didn't mean it's a method with one line of code in the block I meant that it's one line of code that does whatever it does (the method in C).
You need to login to post a reply.