Skip to main content
gamedev.net
🔒 Locked

[java] Java with no Float or Double support! need work around!

Started by Woody FX Jul 19, 2003 at 9:43 AM 8 replies 550+ views
Original Post
Woody FX
Woody FX
I have an equasion that does screen transformations. But the equation throws up fractions. MIDP has no floating point support. (no double or float) Can fixed point math come to the rescue somehow? ScreenX = 45 * WorldX / WorldZ + 45 ScreenY = 45 * WorldY / WorldZ + 45 if x = 30 and z = 70 ScreenX= 45 * 30/70 + 45 = 45 * .428571 + 45 = 19.285714 + 45 = 64.285714 Using Micro Java there is no support for doubles or floats! Is there a work around that will still give you the answer! ScreenX = 62 would be accurate enough! the main problem that i see is with WorldX/worldZ as this always returns a fraction! Thank Brian
Strife
Strife
Multiply the numerator by 100 just to avoid the less than 1 fraction, then before you add, divide by 100 to get the real result you want.

Of course, this may not be necessary depending on where truncation occurrs...

The Artist Formerly Known as CmndrM

http://chaos.webhop.org
Ditto Brother Rat
Ditto Brother Rat
Actually, uh, doesn''t that code work? If you don''t use brackets then it would go through the equation in order so it does the multiplication first before the division. Basically (45 * worldx) / worldz + 45. Try it, i''m probably wrong.

And are you doing a 3D game for MIDP?
Woody FX
Woody FX
I think division always done first, is''nt there an order that each operator has with addition being last i think!

so what you are saying is that the device will not get rid of the .428571 but hold it while it does the calculation and the just thrown away what ever comes after the decimal place once the complete evaluation of the equation is complete.

So MIDP would come out with the answer of 62???

That would be acceptable if it works?

Can anybody confirm?
abstractworlds
abstractworlds
It''s definitely worth looking into fixed point math for MIDP, there are some libraries or you could grow your own (google for fixed point MIDP).

Fixed point usually involves multiplying your numbers by a set value so the fractional part enters into the integer part to avoid fractions, and then dividing down when you require the rounded number.

As Strife suggests this can be done by physically multiplying and dividing, which could be slow for MIDP, or better still bit shifting up and down with Java''s << and >> operators (e.g. bit shifting left by 7 places is equivalent to multiplying by 2x2x2x2x2x2x2 = 128). Bitshifting is used in most MIDP fixed point libraries.

Strife
Strife
quote:
Original post by Woody FX
I think division always done first, is''nt there an order that each operator has with addition being last i think!


Unless it doesn''t follow standard math rules, multiplication is done first. Remember PEMDAS. Actually, it''s really PEMMDAS.

(Parentheses, exponents, multiplication, modulus, division, addition, subtraction... And actually I''m not sure if modulus is before or after multiplication).

The Artist Formerly Known as CmndrM

http://chaos.webhop.org
CaptainJester
CaptainJester
quote:
Original post by Strife
quote:
Original post by Woody FX
I think division always done first, is'nt there an order that each operator has with addition being last i think!


Unless it doesn't follow standard math rules, multiplication is done first. Remember PEMDAS. Actually, it's really PEMMDAS.

(Parentheses, exponents, multiplication, modulus, division, addition, subtraction... And actually I'm not sure if modulus is before or after multiplication).

The Artist Formerly Known as CmndrM

http://chaos.webhop.org


You are not entirely correct.

divide, modulus and multiply are on the same level. So which ever one is encountered first is done first.

The quick and easy test for this is:

public class TestApp {

public static void main(String args[]) {
System.out.println("x="+(45 * 30/70 + 45) );
}
}


This returns a result of 64.

quote:
From the Java 2 certification study guide
Order of operations in Java

Unary ++ -- + - ! ~ ()

Arithmetic * / %
+ -

Shift << >> >>>

Comparison < <= > >= instanceof
== !=

Bitwise & ^ |

Short-circuit && ||

Ternary ?:

Assignment = 'op='






First make it work,
then make it fast.

--Brian Kernighan

"I’m happy to share what I can, because I’m in it for the love of programming. The Ferraris are just gravy, honest!" --John Carmack: Forward to Graphics Programming Black Book

[edited by - CaptainJester on July 19, 2003 7:04:26 PM]
"None of us learn in a vacuum; we all stand on the shoulders of giants such as Wirth and Knuth and thousands of others. Lend your shoulders to building the future!" - Michael Abrash[JavaGaming.org][The Java Tutorial][Slick][LWJGL][
Strife
Strife
quote:
Original post by CaptainJester
You are not entirely correct.

divide, modulus and multiply are on the same level. So which ever one is encountered first is done first.

Yes, this is actually what I meant. But thanks for correcting me.

The Artist Formerly Known as CmndrM

http://chaos.webhop.org
Woody FX
Woody FX
Did a test and the equation works ok in MIDP!

Thanks

But for the future i should probably look in to learning more about bit shifting.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.