floating point

B

bob smith

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?
 
M

markspace

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.
 
L

Lew

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

Excellent advice.

"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."

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.

<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>
 
M

Martin Gregorie

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
 
G

Gene Wirchenko

[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.

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

Sincerely,

Gene Wirchenko
 
J

Jukka Lahtinen

Patricia Shanahan said:
On 7/24/2012 12:57 PM, bob smith wrote:
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.
 
L

Lew

Gene said:
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.

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.
 
A

Arne Vajhøj

..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
 
G

Gene Wirchenko

Gene said:
Lew wrote:
[snip]
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
truth in order to mislead) I suggest that a Java newbie start with an
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
 
R

Roedy Green

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
--
Roedy Green Canadian Mind Products
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)
 
L

Lew

Gene said:
You prevaricate very well. (prevaricate: to selectively tell the
truth in order to mislead) I suggest that a Java newbie start with an
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
ad hominem attacks.

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.
 
G

Gene Wirchenko

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
order to mislead.
But you have rejected it, repeatedly.

Only for someone just starting out.
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
ad hominem attacks.

"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
 
A

Arne Vajhøj

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" 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
to read explanation.

Arne
 
J

Jan Burse

Hi,

Gene said:
"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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top