Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
Solved

Floats

Posted on 1998-08-27
Medium Priority
318 Views
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
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

LVL 3

Accepted Solution

sailwind earned 120 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

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

ID: 1222770
Which of the two take up more mem?
I suppose it's double eh?

Thanks again.
0

LVL 3

Expert Comment

ID: 1222771
int 32 bits
double 64 bits
0

LVL 3

Expert Comment

ID: 1222772
float 32 bits
long 64 bits
0

Featured Post

Question has a verified solution.

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

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Suggested Courses
Course of the Month7 days, 1 hour left to enroll

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

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