Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > floating point

Reply
Thread Tools

floating point

 
 
bob smith
Guest
Posts: n/a
 
      07-24-2012
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?


 
Reply With Quote
 
 
 
 
markspace
Guest
Posts: n/a
 
      07-24-2012
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.



 
Reply With Quote
 
 
 
 
Lew
Guest
Posts: n/a
 
      07-24-2012
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


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.

"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

 
Reply With Quote
 
Martin Gregorie
Guest
Posts: n/a
 
      07-24-2012
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 |
 
Reply With Quote
 
Gene Wirchenko
Guest
Posts: n/a
 
      07-24-2012
On Tue, 24 Jul 2012 13:21:33 -0700 (PDT), Lew <(E-Mail Removed)>
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.
>
>"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
 
Reply With Quote
 
Jukka Lahtinen
Guest
Posts: n/a
 
      07-25-2012
Patricia Shanahan <(E-Mail Removed)> 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
 
Reply With Quote
 
Lew
Guest
Posts: n/a
 
      07-25-2012
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.
>>
>> "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.
http://upload.wikimedia.org/wikipedi.../c/cf/Friz.jpg
 
Reply With Quote
 
Arne Vajhj
Guest
Posts: n/a
 
      07-25-2012
On 7/25/2012 5:22 AM, Jukka Lahtinen wrote:
> Patricia Shanahan <(E-Mail Removed)> 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


 
Reply With Quote
 
Gene Wirchenko
Guest
Posts: n/a
 
      07-25-2012
On Wed, 25 Jul 2012 06:33:24 -0700, Lew <(E-Mail Removed)> 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
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
 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      07-25-2012
On Wed, 25 Jul 2012 12:22:02 +0300, Jukka Lahtinen
<(E-Mail Removed)> 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
--
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)
http://www.youtube.com/watch?v=F-QA2rkpBSY


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Share-Point-2010 ,Share-Point -2010 Training , Share-point-2010Hyderabad , Share-point-2010 Institute Saraswati lakki ASP .Net 0 01-06-2012 06:39 AM
floating point problem... floating indeed :( teeshift Ruby 2 12-01-2006 01:16 AM
converting floating point to fixed point H aka N VHDL 15 03-02-2006 02:26 PM
floating point to fixed point conversion riya1012@gmail.com C Programming 4 02-22-2006 05:56 PM
Fixed-point format for floating-point numbers Motaz Saad Java 7 11-05-2005 05:33 PM



Advertisments