Solved

Floats

Posted on 1998-08-27
5
300 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
  • 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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Suggested Solutions

For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

744 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now