merlin bar

Ranch Hand

Posts: 54

posted 13 years ago

my method takes two arguments

(int maxCapacity, int currentCapacity )

so if a 100 litre tank is half full, you do

(100, 50)

However, I want to convert this

This doesnt work for me.

float asInverseFraction = capacity/level + (capacity%level)/100;

Thanks in advance,

merlin

(int maxCapacity, int currentCapacity )

so if a 100 litre tank is half full, you do

(100, 50)

However, I want to convert this

*scale*to a 0 - 75 scale. Rounding errors are giving me hell. Can anyone help?This doesnt work for me.

float asInverseFraction = capacity/level + (capacity%level)/100;

Thanks in advance,

merlin

Regards,<br /> merlin_bar

merlin bar

Ranch Hand

Posts: 54

Dirk Schreckmann

Sheriff

Posts: 7023

merlin bar

Ranch Hand

Posts: 54

posted 13 years ago

OK, my method takes the max value possible, and the actual value within this limit.

TotalCapacity / currentVolume omits the remainder so I dont get a proper ratio.

Eg

100 / 75 = 1 //in java

if I do 100 % 75 I get 25.

The correct answer for 100 / 75 is 1.3333......

How do I get this?

TotalCapacity / currentVolume omits the remainder so I dont get a proper ratio.

Eg

100 / 75 = 1 //in java

if I do 100 % 75 I get 25.

The correct answer for 100 / 75 is 1.3333......

How do I get this?

Regards,<br /> merlin_bar

Michael Morris

Ranch Hand

Posts: 3451

posted 13 years ago

If you want to convert an int (or long) for floating point operations without loss of precision, you can use the java.math.BigInteger and java.math.BigDecimal classes. For example:

Once you convert back to the double then precision will be lost of course. You can also set the scale (or precision) of the BigDecimal to any arbitrary length. Take a look at the API docs and see if this may help to solve your problem.

[ December 07, 2003: Message edited by: Michael Morris ]

Once you convert back to the double then precision will be lost of course. You can also set the scale (or precision) of the BigDecimal to any arbitrary length. Take a look at the API docs and see if this may help to solve your problem.

[ December 07, 2003: Message edited by: Michael Morris ]

*Any intelligent fool can make things bigger, more complex, and more violent. It takes a touch of genius - and a lot of courage - to move in the opposite direction.* - Ernst F. Schumacher

Dirk Schreckmann

Sheriff

Posts: 7023

posted 13 years ago

I just want to point out the simple answer, just in case you're not aware of it.

Using the code in your second post, if you want the division expression to result in a floating point number, just convert one or both of the integral operands before the division operation --

Now, it won't be 1.333... with the 3's repeating forever, because Java is running on a computer and there is a need to represent data (and numbers) in a finite amount of space, and so begins the not-so-simple answer. The bit pattern used for floating point numbers (IEEE 754) doesn't provide for never-ending values to be represented. Instead, it makes an approximation that can be stored in the amount of space allocated (which in the case of floats is 4 bytes).

Even if you were to work with BigDecimal, which can represent floating point numbers to an arbitrary degree of precision, the never-ending 3's would still need to be truncated at some point. If float or double don't offer enough decimal points for you, then use BigDecimal and specify your own number of decimal points.

Is your solution becoming clearer?

[ December 08, 2003: Message edited by: Dirk Schreckmann ]

Using the code in your second post, if you want the division expression to result in a floating point number, just convert one or both of the integral operands before the division operation --

*(float)capacity/level*-- and the result will be a floating point number.Now, it won't be 1.333... with the 3's repeating forever, because Java is running on a computer and there is a need to represent data (and numbers) in a finite amount of space, and so begins the not-so-simple answer. The bit pattern used for floating point numbers (IEEE 754) doesn't provide for never-ending values to be represented. Instead, it makes an approximation that can be stored in the amount of space allocated (which in the case of floats is 4 bytes).

Even if you were to work with BigDecimal, which can represent floating point numbers to an arbitrary degree of precision, the never-ending 3's would still need to be truncated at some point. If float or double don't offer enough decimal points for you, then use BigDecimal and specify your own number of decimal points.

Is your solution becoming clearer?

[ December 08, 2003: Message edited by: Dirk Schreckmann ]

merlin bar

Ranch Hand

Posts: 54

posted 13 years ago

Thanks folks, that answers my question. I wasnt aware of the BigDecimal classes, but Dirk, that was what I really wanted, converting just before the calculation does the job.

Thanks again!

merlin

[ December 08, 2003: Message edited by: merlin bar ]

Thanks again!

merlin

[ December 08, 2003: Message edited by: merlin bar ]

Regards,<br /> merlin_bar