Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Java (http://www.velocityreviews.com/forums/f30-java.html)
-   -   floating point (http://www.velocityreviews.com/forums/t948495-floating-point.html)

 bob smith 07-24-2012 07:57 PM

floating point

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?

 markspace 07-24-2012 08:01 PM

Re: floating point

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.

 Lew 07-24-2012 08:21 PM

Re: floating point

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

 Martin Gregorie 07-24-2012 08:34 PM

Re: floating point

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 |

 Gene Wirchenko 07-24-2012 08:50 PM

Re: floating point

On Tue, 24 Jul 2012 13:21:33 -0700 (PDT), Lew <lewbloch@gmail.com>
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

 Jukka Lahtinen 07-25-2012 09:22 AM

Re: floating point

Patricia Shanahan <pats@acm.org> 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

 Lew 07-25-2012 01:33 PM

Re: floating point

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.

 Arne Vajhøj 07-25-2012 02:54 PM

Re: floating point

On 7/25/2012 5:22 AM, Jukka Lahtinen wrote:
> Patricia Shanahan <pats@acm.org> 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

 Gene Wirchenko 07-25-2012 05:44 PM

Re: floating point

On Wed, 25 Jul 2012 06:33:24 -0700, Lew <noone@lewscanon.com> 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

 Roedy Green 07-25-2012 08:29 PM

Re: floating point

On Wed, 25 Jul 2012 12:22:02 +0300, Jukka Lahtinen
<jtfjdehf@hotmail.com.invalid> 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
--