# floating point

Discussion in 'Java' started by bob smith, Jul 24, 2012.

1. ### bob smithGuest

I can never remember this.

Let's say you have an integer and a float in an operation. Is the result always a float?

For instance;

int x = 5;

Is x/10.0f definitely a float? Is this true for all ops?

bob smith, Jul 24, 2012

2. ### markspaceGuest

On 7/24/2012 12:57 PM, bob smith wrote:
> I can never remember this.
>
> Let's say you have an integer and a float in an operation. Is the result always a float?

You can just try this to see what you get, or read the JLS. From
memory, I'm 90% sure that ints will be promoted to floats. I'm not sure
how order of operations affects this. In the expression (1+1)/2.0f,
does the addition use floats or ints? I'm not sure, but I think ints.

markspace, Jul 24, 2012

3. ### LewGuest

markspace wrote:
> bob smith wrote:
>> I can never remember this.
>>
>> Let's say you have an integer and a float in an operation. Is the result always a float?

>
>
> You can just try this to see what you get, or read the JLS. From

"Just trying it" could obscure some of the intermediate promotions.

> memory, I'm 90% sure that ints will be promoted to floats. I'm not sure
> how order of operations affects this. In the expression (1+1)/2.0f,
> does the addition use floats or ints? I'm not sure, but I think ints.

From the JLS:

"The left-hand operand of a binary operator appears to be fully evaluated
before any part of the right-hand operand is evaluated."

"The Java programming language respects the order of evaluation indicated
explicitly by parentheses and implicitly by operator precedence."

"5.6. Numeric Promotions

"Numeric promotion is applied to the operands of an arithmetic operator.
Numeric promotion contexts allow the use of:

" an identity conversion (§5.1.1)
" a widening primitive conversion (§5.1.2)
" an unboxing conversion (§5.1.8)

"Numeric promotions are used to convert the operands of a numeric
operator to a common type so that an operation can be performed.
The two kinds of numeric promotion are unary numeric promotion
(§5.6.1) and binary numeric promotion (§5.6.2)."

So, by those sections you can see that the division must be evaluated
as 'float'.

For those who decry the use of the JLS to learn this, just note that the
relevant sections are not especially obscure and they answer the
question completely and authoritatively. But feel free to struggle with
alternatives if you really want to work harder.

<http://docs.oracle.com/javase/specs/jls/se7/html/jls-15.html>
<http://docs.oracle.com/javase/specs/jls/se7/html/jls-15.html#jls-15.7>
<http://docs.oracle.com/javase/specs/jls/se7/html/jls-5.html#jls-5.6>
<http://docs.oracle.com/javase/specs/jls/se7/html/jls-5.html#jls-5.6.2>

--
Lew

Lew, Jul 24, 2012
4. ### Martin GregorieGuest

On Tue, 24 Jul 2012 13:01:11 -0700, markspace wrote:

> On 7/24/2012 12:57 PM, bob smith wrote:
>> I can never remember this.
>>
>> Let's say you have an integer and a float in an operation. Is the
>> result always a float?

>
>
> You can just try this to see what you get, or read the JLS. From
> memory, I'm 90% sure that ints will be promoted to floats. I'm not sure
> how order of operations affects this. In the expression (1+1)/2.0f,
> does the addition use floats or ints? I'm not sure, but I think ints.

I think this:

\$ cat MathTest.java
public class MathTest
{
public static void main(String[] args)
{
double x = (9 / 2) / 5;
System.out.println("x = " + x);
double y = (9 / 2) / 5.0;
System.out.println("y = " + y);
double z = (9 / 2.0) / 5.0;
System.out.println("z = " + z);
}
}
\$ javac MathTest.java
\$ java MathTest
x = 0.0
y = 0.8
z = 0.9

shows pretty conclusively that widening from int to double isn't done
until the last possible moment.

The calculation of x is entirely integral, with widening to double only
occurring at the assignment. Calculating y can only produce that result
if the first division was integral and its result was then widened to
double before the second division. And, of course, the z calculation was
entirely done as floating point

--
martin@ | Martin Gregorie
gregorie. | Essex, UK
org |

Martin Gregorie, Jul 24, 2012
5. ### Gene WirchenkoGuest

On Tue, 24 Jul 2012 13:21:33 -0700 (PDT), Lew <>
wrote:

[snip]

>
>"5.6. Numeric Promotions
>
>"Numeric promotion is applied to the operands of an arithmetic operator.
>Numeric promotion contexts allow the use of:
>
>" an identity conversion (§5.1.1)
>" a widening primitive conversion (§5.1.2)
>" an unboxing conversion (§5.1.8)
>
>"Numeric promotions are used to convert the operands of a numeric
>operator to a common type so that an operation can be performed.
>The two kinds of numeric promotion are unary numeric promotion
>(§5.6.1) and binary numeric promotion (§5.6.2)."
>
>So, by those sections you can see that the division must be evaluated
>as 'float'.
>
>For those who decry the use of the JLS to learn this, just note that the
>relevant sections are not especially obscure and they answer the
>question completely and authoritatively. But feel free to struggle with
>alternatives if you really want to work harder.

This sort of thing is handled in many introductory programming
language texts so it is not harder at all to use them instead.

><http://docs.oracle.com/javase/specs/jls/se7/html/jls-15.html>
><http://docs.oracle.com/javase/specs/jls/se7/html/jls-15.html#jls-15.7>
><http://docs.oracle.com/javase/specs/jls/se7/html/jls-5.html#jls-5.6>
><http://docs.oracle.com/javase/specs/jls/se7/html/jls-5.html#jls-5.6.2>

Whereas in a text, it might be "Chapter 3: Datatypes and
Operations". A bit simpler, no?

Sincerely,

Gene Wirchenko

Gene Wirchenko, Jul 24, 2012
6. ### Jukka LahtinenGuest

Patricia Shanahan <> writes:
> On 7/24/2012 12:57 PM, bob smith wrote:

>> Let's say you have an integer and a float in an operation. Is the

> May I ask why you are using float? Because of its very limited
> precision, it is better to use double unless there is a specific reason

...and even better to use BigDecimal, if you need all the decimals to be
correct, like when calculating any amounts of money.

Of course, there are situations where rounding errors don't matter and
double is good enough.
Like, I suppose, some statistical calculations.

--
Jukka Lahtinen

Jukka Lahtinen, Jul 25, 2012
7. ### LewGuest

Gene Wirchenko wrote:
> Lew wrote:
>
> [snip]
>
>> So, yes, the parenthesized addition is done as an 'int' addition.
>>
>> "5.6. Numeric Promotions
>>
>> "Numeric promotion is applied to the operands of an arithmetic operator.
>> Numeric promotion contexts allow the use of:
>>
>> " an identity conversion (Â§5.1.1)
>> " a widening primitive conversion (Â§5.1.2)
>> " an unboxing conversion (Â§5.1.8)
>>
>> "Numeric promotions are used to convert the operands of a numeric
>> operator to a common type so that an operation can be performed.
>> The two kinds of numeric promotion are unary numeric promotion
>> (Â§5.6.1) and binary numeric promotion (Â§5.6.2)."
>>
>> So, by those sections you can see that the division must be evaluated
>> as 'float'.
>>
>> For those who decry the use of the JLS to learn this, just note that the
>> relevant sections are not especially obscure and they answer the
>> question completely and authoritatively. But feel free to struggle with
>> alternatives if you really want to work harder.

>
> This sort of thing is handled in many introductory programming
> language texts so it is not harder at all to use them instead.
>
>> <http://docs.oracle.com/javase/specs/jls/se7/html/jls-15.html>
>> <http://docs.oracle.com/javase/specs/jls/se7/html/jls-15.html#jls-15.7>
>> <http://docs.oracle.com/javase/specs/jls/se7/html/jls-5.html#jls-5.6>
>> <http://docs.oracle.com/javase/specs/jls/se7/html/jls-5.html#jls-5.6.2>

>
> Whereas in a text, it might be "Chapter 3: Datatypes and
> Operations". A bit simpler, no?

No.

One place to find all the answers, authoritative and final. Versus many places
of dubious quality that might at best indirectly hint at the answer. Victory: JLS.

But you have a bug up your arse about the JLS and I recognize that from your
continued efforts to speak against its use.

I don't demand that anyone read the JLS. I just point out that it has the
answers, that for the most part it's readable and that the more obscure parts
are amenable to study, and that it's worthwhile. Those who excoriate its use,
as opposed to, say, recommending that one work with additional material as
well, are leading the rest astray. I really don't understand this rabid
rejection of such a useful resource.

Those of you without an irrational fear of the JLS should judge for
yourselves. Don't be afraid of it because the Genes of the world are trying to
frighten you away from its use. Oh, don't use it as your only source, not even
after you've gained familiarity with it and with Java, but by all means use
it. Gene is wrong to discourage that.

--
Lew
Honi soit qui mal y pense.

Lew, Jul 25, 2012
8. ### Arne VajhøjGuest

On 7/25/2012 5:22 AM, Jukka Lahtinen wrote:
> Patricia Shanahan <> writes:
>> On 7/24/2012 12:57 PM, bob smith wrote:

>
>>> Let's say you have an integer and a float in an operation. Is the

>
>> May I ask why you are using float? Because of its very limited
>> precision, it is better to use double unless there is a specific reason

>
> ..and even better to use BigDecimal, if you need all the decimals to be
> correct, like when calculating any amounts of money.
>
> Of course, there are situations where rounding errors don't matter and
> double is good enough.
> Like, I suppose, some statistical calculations.

Anything that is measured with uncertainty.

Arne

Arne Vajhøj, Jul 25, 2012
9. ### Gene WirchenkoGuest

On Wed, 25 Jul 2012 06:33:24 -0700, Lew <> wrote:

>Gene Wirchenko wrote:
>> Lew wrote:

[snip]

>>> <http://docs.oracle.com/javase/specs/jls/se7/html/jls-15.html>
>>> <http://docs.oracle.com/javase/specs/jls/se7/html/jls-15.html#jls-15.7>
>>> <http://docs.oracle.com/javase/specs/jls/se7/html/jls-5.html#jls-5.6>
>>> <http://docs.oracle.com/javase/specs/jls/se7/html/jls-5.html#jls-5.6.2>

>>
>> Whereas in a text, it might be "Chapter 3: Datatypes and
>> Operations". A bit simpler, no?

>
>No.
>
>One place to find all the answers, authoritative and final. Versus many places
>of dubious quality that might at best indirectly hint at the answer. Victory: JLS.

Authoritative and final:
"One Ring to rule them all
And in the Darkness bind them."

>But you have a bug up your arse about the JLS and I recognize that from your
>continued efforts to speak against its use.

Actually, you do.

Perish the thought that someone starting in Java might not find
the JLS all that readable. Perish the thought that someone might be
able to explain something better than is done in the JLS.

You prevaricate very well. (prevaricate: to selectively tell the
introductory text and use the JLS later.

>I don't demand that anyone read the JLS. I just point out that it has the
>answers, that for the most part it's readable and that the more obscure parts
>are amenable to study, and that it's worthwhile. Those who excoriate its use,
>as opposed to, say, recommending that one work with additional material as
>well, are leading the rest astray. I really don't understand this rabid
>rejection of such a useful resource.

But we do not reject it. By stating that we do, you get to score
points. That sort of behaviour has gotten old.

>Those of you without an irrational fear of the JLS should judge for
>yourselves. Don't be afraid of it because the Genes of the world are trying to
>frighten you away from its use. Oh, don't use it as your only source, not even
>after you've gained familiarity with it and with Java, but by all means use
>it. Gene is wrong to discourage that.

Again, I do not do that.

Lew, why do you keep turning things into unpleasant arguments?
There is a big difference between disagreeing and being disagreeable.
How about trying the former for a change?

Sincerely,

Gene Wirchenko

Gene Wirchenko, Jul 25, 2012
10. ### Roedy GreenGuest

On Wed, 25 Jul 2012 12:22:02 +0300, Jukka Lahtinen
<> wrote, quoted or indirectly quoted
someone who said :

>Of course, there are situations where rounding errors don't matter and
>double is good enough.
>Like, I suppose, some statistical calculations.

If something represents a measurement, e.g. the length of a desk, then
double will nearly always suffice. When doing math on fat integers,
like computing pi, or doing encryption then you want BigInteger
--
http://mindprod.com
The greatest shortcoming of the human race is our inability to understand the exponential function.
~ Dr. Albert A. Bartlett (born: 1923-03-21 age: 89)

Roedy Green, Jul 25, 2012
11. ### LewGuest

Gene Wirchenko wrote:
> Lew wrote:
>> Perish the thought that someone starting in Java might not find
>> the JLS all that readable. Perish the thought that someone might be
>> able to explain something better than is done in the JLS.

>
> You prevaricate very well. (prevaricate: to selectively tell the
> introductory text and use the JLS later.

I never said a newbie should avoid introductory texts. Nor do I suggest
that a newbie rely on the JLS. You have misunderstood my points yet again.

I agree that introductory texts are great to introduce Java.

My objection is to the active discouragement of the use of the JLS.

I recommend that newbies be aware of the JLS and practice reading
it from an early age. That is not so inconsistent with your recommendation.

As for calling me a liar, screw you.

> &gt;I don't demand that anyone read the JLS. I just point out that it has the
> &gt;answers, that for the most part it's readable and that the more obscure parts
> &gt;are amenable to study, and that it's worthwhile. Those who excoriate its use,
> &gt;as opposed to, say, recommending that one work with additional material as
> &gt;well, are leading the rest astray. I really don't understand this rabid
> &gt;rejection of such a useful resource.
>
> But we do not reject it. By stating that we do, you get to score
> points. That sort of behaviour has gotten old.

But you have rejected it, repeatedly.

>
> &gt;Those of you without an irrational fear of the JLS should judge for
> &gt;yourselves. Don't be afraid of it because the Genes of the world are trying to
> &gt;frighten you away from its use. Oh, don't use it as your only source, not even
> &gt;after you've gained familiarity with it and with Java, but by all means use
> &gt;it. Gene is wrong to discourage that.
>
> Again, I do not do that.
>
> Lew, why do you keep turning things into unpleasant arguments?
> There is a big difference between disagreeing and being disagreeable.
> How about trying the former for a change?

How about you stop trying to frighten folks away from the JLS?

If you agree with me that it's a valuable resource of which to be aware,
and that one should aspire to its use eventually, then why do you argue
with me?

It is you who resorts to name calling and unpleasantness. It is a raw
tactic for you to accuse me of being "disagreeable" because you don't
agree that the JLS is a valuable resource. I am disagreeing so adamantly
because your continued speech against it is likely to discourage those
who would most benefit from it. You are performing a disservice to the
Java community, and when called on it, resort to name-calling and

I am only suggesting a balanced approach wherein from the very
start a Java programmer is aware of the definitive definition of
the language, and of the value of being able to use it. I am astounded
that anyone finds that objectionable, much less resorts to the sort
of underhanded responses in which you engaged.

--
Lew

Lew, Jul 26, 2012
12. ### Gene WirchenkoGuest

On Wed, 25 Jul 2012 17:09:27 -0700 (PDT), Lew <>
wrote:

>Gene Wirchenko wrote:
>> Lew wrote:
>>> Perish the thought that someone starting in Java might not find
>>> the JLS all that readable. Perish the thought that someone might be
>>> able to explain something better than is done in the JLS.

>>
>> You prevaricate very well. (prevaricate: to selectively tell the
>> introductory text and use the JLS later.

>
>I never said a newbie should avoid introductory texts. Nor do I suggest
>that a newbie rely on the JLS. You have misunderstood my points yet again.
>
>I agree that introductory texts are great to introduce Java.

Do you agree that intro texts are not the JLS?

>My objection is to the active discouragement of the use of the JLS.

So which is it? When I state that a newbie should go with intro
texts, I am stating how a newbie should be introduced to Java. The
JLS can come later.

>I recommend that newbies be aware of the JLS and practice reading
>it from an early age. That is not so inconsistent with your recommendation.

No, it is not. I just think that starting with it will cause
more trouble than it is worth.

>As for calling me a liar, screw you.

This is typical. I did not call you a liar. I stated that you
prevaricate. Again, prevaricate: to selectively tell the truth in

>> &gt;I don't demand that anyone read the JLS. I just point out that it has the
>> &gt;answers, that for the most part it's readable and that the more obscure parts
>> &gt;are amenable to study, and that it's worthwhile. Those who excoriate its use,
>> &gt;as opposed to, say, recommending that one work with additional material as
>> &gt;well, are leading the rest astray. I really don't understand this rabid
>> &gt;rejection of such a useful resource.
>>
>> But we do not reject it. By stating that we do, you get to score
>> points. That sort of behaviour has gotten old.

>
>But you have rejected it, repeatedly.

Only for someone just starting out.

>> &gt;Those of you without an irrational fear of the JLS should judge for
>> &gt;yourselves. Don't be afraid of it because the Genes of the world are trying to
>> &gt;frighten you away from its use. Oh, don't use it as your only source, not even
>> &gt;after you've gained familiarity with it and with Java, but by all means use
>> &gt;it. Gene is wrong to discourage that.
>>
>> Again, I do not do that.
>>
>> Lew, why do you keep turning things into unpleasant arguments?
>> There is a big difference between disagreeing and being disagreeable.
>> How about trying the former for a change?

>
>How about you stop trying to frighten folks away from the JLS?

I do not. I just suggest later, after the intro texts.

>If you agree with me that it's a valuable resource of which to be aware,
>and that one should aspire to its use eventually, then why do you argue
>with me?

That is odd. *I* am the one who keeps stating eventually, and
you keep giving me trouble over it.

>It is you who resorts to name calling and unpleasantness. It is a raw
>tactic for you to accuse me of being "disagreeable" because you don't
>agree that the JLS is a valuable resource. I am disagreeing so adamantly
>because your continued speech against it is likely to discourage those
>who would most benefit from it. You are performing a disservice to the
>Java community, and when called on it, resort to name-calling and

"disagreeing adamantly" is often disagreeable. Stating that I
have "an irrational fear of the JLS" is name-calling on your part.

>I am only suggesting a balanced approach wherein from the very
>start a Java programmer is aware of the definitive definition of
>the language, and of the value of being able to use it. I am astounded
>that anyone finds that objectionable, much less resorts to the sort
>of underhanded responses in which you engaged.

Prevarication is underhanded, and you have been busy doing it.

I have consistently stated that a newbie should wait on using the
JLS. I have never stated that it should not be used.

Sincerely,

Gene Wirchenko

Gene Wirchenko, Jul 26, 2012
13. ### Arne VajhÃ¸jGuest

On 7/25/2012 9:33 AM, Lew wrote:
> Gene Wirchenko wrote:
>> Lew wrote:
>>> For those who decry the use of the JLS to learn this, just note that the
>>> relevant sections are not especially obscure and they answer the
>>> question completely and authoritatively. But feel free to struggle with
>>> alternatives if you really want to work harder.

>>
>> This sort of thing is handled in many introductory programming
>> language texts so it is not harder at all to use them instead.
>>
>>> <http://docs.oracle.com/javase/specs/jls/se7/html/jls-15.html>
>>> <http://docs.oracle.com/javase/specs/jls/se7/html/jls-15.html#jls-15.7>
>>> <http://docs.oracle.com/javase/specs/jls/se7/html/jls-5.html#jls-5.6>
>>> <http://docs.oracle.com/javase/specs/jls/se7/html/jls-5.html#jls-5.6.2>

>>
>> Whereas in a text, it might be "Chapter 3: Datatypes and
>> Operations". A bit simpler, no?

>
> No.
>
> One place to find all the answers, authoritative and final. Versus many
> places of dubious quality that might at best indirectly hint at the

"authoritative and final" does not imply "not harder" or "simpler".

JLS is by definition the authoritative source and should be consulted
if there are doubt in other sources or implementations.

But it does not mean that a Java beginners guide can not have an easier

Arne

Arne VajhÃ¸j, Jul 26, 2012
14. ### Jan BurseGuest

Hi,

Gene Wirchenko schrieb:
>> You are performing a disservice to the
>> >Java community, and when called on it, resort to name-calling and

> "disagreeing adamantly" is often disagreeable. Stating that I
> have "an irrational fear of the JLS" is name-calling on your part.
>

3rd time I observe this already on c.l.j.p today,
troll against troll. It could be only that it is
summertime, and everybody is on the beach except
for the trolls (and me having back pain).

Ha Ha. Nearly makes my day.

Bye

Jan Burse, Jul 26, 2012