Thanks all for being patient!
@Patricia
I agree, for display. However, you seem to be trying to impose on
intermediate results the sort of rounding that would normally only be
applied on display. That makes sense in some contexts, such as
financial
transactions which have specified rounding rules that have to be
applied
at specific stages in the calculation. However, it will generally
reduce
the accuracy of the result.
<
I don't suggest this should be the rule in all (in fact - in any)
calculation. Just commented on the need to present the numbers in the
human readable form. This is always the case AFAIK. Maybe I need to be
more clearer - at the end I don't care if I have 1 kg or
0.9999992304981034 kg of sugar when I leave the grocery store. The
additional information is not just useless, but even harms me (very
unreadable, especially if you have 100s of rows to display, for
example). If somebody needs this (for precise scientific
calculations), that's OK - he should use double. However, if I convert
1 from float to double, I don't want it extended to something else, as
Josh pointed out. Again, I am aware there is not enough information to
"decide" which of the "extension possibilities" to take, but this
still doesn't solve the problem, unfortunately.
Backing up a stage, if you need to impose rounding to a limited number
of decimal digits at intermediate stages,
<
I don't. If I wanted to round, I could use Math.rint(f * 1000000) /
1000000 or something like that. I want float extended to double, but I
just don't want to take what may be the most logical way to extend
float to double. In my case - and I presume a lot of other cases,
extending float to double would be most logically done in base 10, not
base 2. May I call this a mishap that computers don't work in our
base?
@Owen
There is, however, a BigDecimal (String) constructor
<
Sure, but I'd still need float -> string. Taking that into account,
it's just extra work: instead of float -> string -> double, I'd have
to do float -> string -> BigDecimal -> double. I don't need to do any
calculations, so having it in BigDecimal gains me nothing. The only
thing that would be beneficial is if I could go skip the toString step
- it would be a little cleaner solution dealing with numbers only.
@Lew
Patricia's comment was not about displaying in binary but thinking in binary.
An understanding of binary computation is important for numerical analysis,
the study of how computers do computations. In your original post you
stated:
I don't want rounding or any other things that would interfere with
the result in any way
Understanding finite-precision binary representation, one groks that
all
floating-point representations in a computer, except for a finite set
of
values, are rounded. Therefore rounding will be inevitable when
dealing with
'float' and 'double' values. Thinking in terms of binary
representation, one
understands why the 'float' value was displayed as 1.234567, but the
double
value as 1.2345670461654663.
<
I agree with everything. Still, the problem is this:
- You input the number in base 10
- It gets into float f
- I get that float f
- I need to send that as a double d
- To do that, I need to do: double d = (double)f
- Whether the above is explicit or implicit, it will be converted to
the double representation of whatever was stored in float
- The problem is that all values are rounded, as Josh and you noted,
so I don't get the value I wanted, but the nearest possible
- At the end, I need the same number in base 10.
The question is - how do I get this one? May I restate the whole
problem here, after sorting out all the misunderstandings I created.
Say you have this:
- Input a number in base 10
- Store it in a float
- Print this float (to take into account the possibility the number
cannot be represented in float accurately enough)
- Call a method which takes double and returns it's input unmodified
as a double
- Print out the result in base 10
- Confirm that what is displayed is exactly the same as what was
printed in step 3
What are solutions to this? I showed one. Are there others? Why are
they better? Personally, I don't like going through String, that's why
I'm asking at the first place.