• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 323
  • Last Modified:

Floats

Hi,

I'm using Visual J++,
and I can't seem to get floats working right!
Here's a piece of code:
float f = 150 / 255;
g.drawString(new Float(f).toString,10,10);

It says 0.0
I'm no mathematician but I'm pretty sure that 150/255 isn't 0.

What's wrong?

Thanks
0
BabyFace
Asked:
BabyFace
  • 4
1 Solution
 
sailwindCommented:
150 and 255 are both integers.
When you divide two integers, you'll get an integer.
0.588 casted into an integer is a 0. That's why you're
getting 0 for output.

You should do:

float f = 150.0/255.0;   // now you're dividing float by float

That'll correct the problem.

0
 
sailwindCommented:
Oh yeah, since 150.0 is considered a double in Java. You should do:

double d = 150.0/255.0  


0
 
BabyFaceAuthor Commented:
Thanks for your answer.
Which of the two take up more mem?
I suppose it's double eh?

Thanks again.
0
 
sailwindCommented:
int 32 bits
double 64 bits
0
 
sailwindCommented:
float 32 bits
long 64 bits
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now