Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (499)
Games in Android Showcase (118)
games submitted by our members
Games in WIP (567)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  floats  (Read 2262 times)
0 Members and 1 Guest are viewing this topic.
Offline appel

JGO Wizard


Medals: 51
Projects: 4


I always win!


« Posted 2006-12-17 12:53:15 »

Hi,

I've been looking at the my class file in Jshrink, and I notice that float values like 0.03f get extracted to something like 0.029999999329447746. Clearly, this will take up bytes?
Is there a way to "trick" Java not to do that? I've tried to use (float)0.03  (double that is) but Java outsmarts me and makes it a float.

But why does it do this to floats anyway? Doesn't do it to double :\

Check out the 4K competition @ www.java4k.com
Check out GAMADU (my own site) @ http://gamadu.com/
Offline woogley
« Reply #1 - Posted 2006-12-17 13:04:36 »

you could try strictfp, though i don't know why you're having this problem in the first place.

consider this test:
1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
public class Test {
   public static void main(String args[]) {
      float x = 0.03f;
      System.out.println(x);
      x = new Float(0.03f).floatValue();
      System.out.println(x);
      x = Float.parseFloat("0.03");
      System.out.println(x);
   }
}


output:
Quote
0.03
0.03
0.03
Offline oNyx

JGO Coder


Medals: 2


pixels! :x


« Reply #2 - Posted 2006-12-17 13:07:51 »

>Clearly, this will take up bytes?

As much space as a float or double takes. What you see there is the string representation.

edit:
float f1=0.3f;
float f2=(float)0.3;
double d1=0.333;

becomes:
float f = 0.3F;
float f1 = 0.3F;
double d = 0.33300000000000002D;

弾幕 ☆ @mahonnaiseblog
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline SluX

Junior Member





« Reply #3 - Posted 2006-12-17 13:18:24 »

Yeah, it will take as much space as floating point numbers use for their representation(IEEE 754 standard)...

"Intelligence is the most beautiful gift and the greatest temptation which one life can receive from the gods."Me Cheesy
Play strategic football
Offline appel

JGO Wizard


Medals: 51
Projects: 4


I always win!


« Reply #4 - Posted 2006-12-17 14:28:35 »

I know that float is 32bit, so they take up 4 bytes. But that's in memory! What about in class files?

woogley, I tried that Test... and then I opened the class file in JShrink and this is what I see...:

1  
2  
3  
4  
5  
6  
7  
8  
9  
  static public void main(String[] args) {
    float x;
    x = 0.029999999329447746;
    out.println(x);
    x = new Float(0.029999999329447746).floatValue();
    out.println(x);
    x = Float.parseFloat("0.03");
    out.println(x);
  }

Check out the 4K competition @ www.java4k.com
Check out GAMADU (my own site) @ http://gamadu.com/
Offline woogley
« Reply #5 - Posted 2006-12-17 14:34:12 »

whatever is converting that class (is it your code? or one of the optimizers?) needs to be using strictfp i think. I open it with JAD and the 0.03f remains in the classfile.
Offline nonnus29

Senior Member




Giving Java a second chance after ludumdare fiasco


« Reply #6 - Posted 2006-12-17 17:43:00 »

Quote
I know that float is 32bit, so they take up 4 bytes. But that's in memory! What about in class files?

0.03f  -->  5 characters = 5 bytes

.0.029999999329447746 -->  4 bytes


I'm not seeing a problem here....
Offline appel

JGO Wizard


Medals: 51
Projects: 4


I always win!


« Reply #7 - Posted 2006-12-17 18:28:52 »

0.03f  -->  5 characters = 5 bytes

.0.029999999329447746 -->  4 bytes


I'm not seeing a problem here....

Thanks! Smiley

Check out the 4K competition @ www.java4k.com
Check out GAMADU (my own site) @ http://gamadu.com/
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

Pippogeek (35 views)
2014-09-24 16:13:29

Pippogeek (28 views)
2014-09-24 16:12:22

Pippogeek (17 views)
2014-09-24 16:12:06

Grunnt (40 views)
2014-09-23 14:38:19

radar3301 (23 views)
2014-09-21 23:33:17

BurntPizza (59 views)
2014-09-21 02:42:18

BurntPizza (29 views)
2014-09-21 01:30:30

moogie (34 views)
2014-09-21 00:26:15

UprightPath (47 views)
2014-09-20 20:14:06

BurntPizza (51 views)
2014-09-19 03:14:18
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!