Solved

Floats

Posted on 1998-08-27
5
312 Views
Last Modified: 2012-05-04
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
Comment
Question by:BabyFace
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
5 Comments
 
LVL 3

Accepted Solution

by:
sailwind earned 30 total points
ID: 1222768
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
 
LVL 3

Expert Comment

by:sailwind
ID: 1222769
Oh yeah, since 150.0 is considered a double in Java. You should do:

double d = 150.0/255.0  


0
 
LVL 1

Author Comment

by:BabyFace
ID: 1222770
Thanks for your answer.
Which of the two take up more mem?
I suppose it's double eh?

Thanks again.
0
 
LVL 3

Expert Comment

by:sailwind
ID: 1222771
int 32 bits
double 64 bits
0
 
LVL 3

Expert Comment

by:sailwind
ID: 1222772
float 32 bits
long 64 bits
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

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

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
check java version using powershell 13 441
jsp insert to database example 2 86
Java syntax, or is it Selenium 6 57
Setup Eclipse for Andriod development 2 15
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses

738 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question