codereviewanskquestions
codereviewanskquestions

Reputation: 13998

Java math conversion

I have this code in C++

float result = -log(result)/0.5231-1.0;

And I converted that into Java code like this

float result = (float) (-Math.log(result)/0.5231-1.0);

Is this correct way to convert the c++ code?

Upvotes: 0

Views: 193

Answers (3)

Grigory Katkov
Grigory Katkov

Reputation: 421

Yes, this is correct. But you need to declare "result" above in code.

float result = ... ;    
result = (float) (-Math.log(result)/0.5231-1.0);

BTW there are many java libs for math purposes. E.g. http://commons.apache.org/math/

Upvotes: 0

orlp
orlp

Reputation: 117711

Well, the first C++ statement is illegal. You are defining result, and while initializing you use result.

But yes, the Java code does the same as the C++ code, if we ignore the irrelevant errors.

Upvotes: 5

Vadiklk
Vadiklk

Reputation: 3764

That is not really a question, but yes, the code you've written in Java does the same as your C++ code.

I wouldn't suggest converting big pieces of code from C++ to java.

Upvotes: 0

Related Questions