Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #16309 > unrolled thread

floating point

Started bybob smith <bob@coolfone.comze.com>
First post2012-07-24 12:57 -0700
Last post2012-07-25 13:29 -0700
Articles 15 — 11 participants

Back to article view | Back to comp.lang.java.programmer


Contents

  floating point bob smith <bob@coolfone.comze.com> - 2012-07-24 12:57 -0700
    Re: floating point markspace <-@.> - 2012-07-24 13:01 -0700
      Re: floating point Lew <lewbloch@gmail.com> - 2012-07-24 13:21 -0700
        Re: floating point Gene Wirchenko <genew@ocis.net> - 2012-07-24 13:50 -0700
          Re: floating point Lew <noone@lewscanon.com> - 2012-07-25 06:33 -0700
            Re: floating point Gene Wirchenko <genew@ocis.net> - 2012-07-25 10:44 -0700
              Re: floating point Lew <lewbloch@gmail.com> - 2012-07-25 17:09 -0700
                Re: floating point Gene Wirchenko <genew@ocis.net> - 2012-07-25 17:42 -0700
                  Re: floating point Jan Burse <janburse@fastmail.fm> - 2012-07-26 03:15 +0200
            Re: floating point Arne Vajhøj <arne@vajhoej.dk> - 2012-07-25 21:05 -0400
      Re: floating point Martin Gregorie <martin@address-in-sig.invalid> - 2012-07-24 20:34 +0000
    Re: floating point Patricia Shanahan <pats@acm.org> - 2012-07-24 13:29 -0700
      Re: floating point Jukka Lahtinen <jtfjdehf@hotmail.com.invalid> - 2012-07-25 12:22 +0300
        Re: floating point Arne Vajhøj <arne@vajhoej.dk> - 2012-07-25 10:54 -0400
        Re: floating point Roedy Green <see_website@mindprod.com.invalid> - 2012-07-25 13:29 -0700

#16309 — floating point

Frombob smith <bob@coolfone.comze.com>
Date2012-07-24 12:57 -0700
Subjectfloating point
Message-ID<d94d1dd0-deb5-4259-b5a0-fb3d57e99c55@googlegroups.com>
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?

[toc] | [next] | [standalone]


#16310

Frommarkspace <-@.>
Date2012-07-24 13:01 -0700
Message-ID<jumuu7$6np$1@dont-email.me>
In reply to#16309
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.


[toc] | [prev] | [next] | [standalone]


#16311

FromLew <lewbloch@gmail.com>
Date2012-07-24 13:21 -0700
Message-ID<fdecd225-042b-4aad-9cb3-b23fd669388e@googlegroups.com>
In reply to#16310
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.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

[toc] | [prev] | [next] | [standalone]


#16315

FromGene Wirchenko <genew@ocis.net>
Date2012-07-24 13:50 -0700
Message-ID<7g2u08dsv3dels3k2t28q9vvbp0l0v7bsv@4ax.com>
In reply to#16311
On Tue, 24 Jul 2012 13:21:33 -0700 (PDT), Lew <lewbloch@gmail.com>
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?

Sincerely,

Gene Wirchenko

[toc] | [prev] | [next] | [standalone]


#16336

FromLew <noone@lewscanon.com>
Date2012-07-25 06:33 -0700
Message-ID<juosid$gnk$1@news.albasani.net>
In reply to#16315
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.
http://upload.wikimedia.org/wikipedia/commons/c/cf/Friz.jpg

[toc] | [prev] | [next] | [standalone]


#16354

FromGene Wirchenko <genew@ocis.net>
Date2012-07-25 10:44 -0700
Message-ID<5kb018t2usi7hl5dvdme6rpig91qb24iul@4ax.com>
In reply to#16336
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
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

[toc] | [prev] | [next] | [standalone]


#16378

FromLew <lewbloch@gmail.com>
Date2012-07-25 17:09 -0700
Message-ID<4f86d614-5a3a-4570-927e-8ce63d648e34@googlegroups.com>
In reply to#16354
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
> 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&#39;t demand that anyone read the JLS. I just point out that it has the 
> &gt;answers, that for the most part it&#39;s readable and that the more obscure parts 
> &gt;are amenable to study, and that it&#39;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&#39;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&#39;t be afraid of it because the Genes of the world are trying to 
> &gt;frighten you away from its use. Oh, don&#39;t use it as your only source, not even 
> &gt;after you&#39;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.

-- 
Lew

[toc] | [prev] | [next] | [standalone]


#16380

FromGene Wirchenko <genew@ocis.net>
Date2012-07-25 17:42 -0700
Message-ID<8441189ph6vofq6m07o8f4b4dmme8u9u2a@4ax.com>
In reply to#16378
On Wed, 25 Jul 2012 17:09:27 -0700 (PDT), Lew <lewbloch@gmail.com>
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
>> 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.

     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.

>> &gt;I don&#39;t demand that anyone read the JLS. I just point out that it has the 
>> &gt;answers, that for the most part it&#39;s readable and that the more obscure parts 
>> &gt;are amenable to study, and that it&#39;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&#39;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&#39;t be afraid of it because the Genes of the world are trying to 
>> &gt;frighten you away from its use. Oh, don&#39;t use it as your only source, not even 
>> &gt;after you&#39;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 
>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

[toc] | [prev] | [next] | [standalone]


#16383

FromJan Burse <janburse@fastmail.fm>
Date2012-07-26 03:15 +0200
Message-ID<juq5o5$916$1@news.albasani.net>
In reply to#16380
Hi,

Gene Wirchenko schrieb:
>> 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.
>

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

[toc] | [prev] | [next] | [standalone]


#16382

FromArne Vajhøj <arne@vajhoej.dk>
Date2012-07-25 21:05 -0400
Message-ID<501097be$0$286$14726298@news.sunsite.dk>
In reply to#16336
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
> 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


[toc] | [prev] | [next] | [standalone]


#16313

FromMartin Gregorie <martin@address-in-sig.invalid>
Date2012-07-24 20:34 +0000
Message-ID<jun0t7$bh5$1@localhost.localdomain>
In reply to#16310
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       |

[toc] | [prev] | [next] | [standalone]


#16312

FromPatricia Shanahan <pats@acm.org>
Date2012-07-24 13:29 -0700
Message-ID<g9Odnb6dKIkkmJLNnZ2dnUVZ_gudnZ2d@earthlink.com>
In reply to#16309
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?
>
> For instance;
>
> int x = 5;
>
> Is x/10.0f definitely a float?  Is this true for all ops?
>
>

It is true for all ops that do binary numeric promotion - see
http://docs.oracle.com/javase/specs/jls/se7/html/jls-5.html#jls-5.6.2

Specifically, neither operand is a double and one operand is a float, so
the other is converted to float. The type of the result is the type of
the operands after that promotion, float.

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
for preferring float and you are sure its precision is sufficient for
the calculation.

Patricia

[toc] | [prev] | [next] | [standalone]


#16334

FromJukka Lahtinen <jtfjdehf@hotmail.com.invalid>
Date2012-07-25 12:22 +0300
Message-ID<m3obn4ci11.fsf@ipa.eternal-september.org>
In reply to#16312
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

[toc] | [prev] | [next] | [standalone]


#16342

FromArne Vajhøj <arne@vajhoej.dk>
Date2012-07-25 10:54 -0400
Message-ID<501008bf$0$294$14726298@news.sunsite.dk>
In reply to#16334
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

[toc] | [prev] | [next] | [standalone]


#16358

FromRoedy Green <see_website@mindprod.com.invalid>
Date2012-07-25 13:29 -0700
Message-ID<djl018li7dlua6d9eh9tj2kvh1vns91ris@4ax.com>
In reply to#16334
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
-- 
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

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.java.programmer


csiph-web