• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

problem with addition

 
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
can some one explain this

please see the picture at http://i43.tinypic.com/zkmlaf.jpg
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Not sure what you are asking... are you questioning why this...



Is not identical to this?




Well, one possibiliity, is that the addition is done in a different order. So, depending on what the types are, it is possible for the result to be different. For example, what if they were all string types?

Henry
 
Sheriff
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And even if we ignore Strings, double / float (in general: floating point) addition may do some rounding between calls. Therefore, a + b + c may be slightly different from a + c + b.
 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rob Prime wrote:And even if we ignore Strings, double / float (in general: floating point) addition may do some rounding between calls. Therefore, a + b + c may be slightly different from a + c + b.




There is no such type of problem of rounding because " + " operator will do automatic widening according to its operands and convert both in the bigger one.

for example ;
 
Henry Wong
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

There is no such type of problem of rounding because " + " operator will do automatic widening according to its operands and convert both in the bigger one.




I believe Rob was referring to precision lost, due to rounding during the operation... try this...



Believe it or not, this will produce slightly different results.... and the reason is because different interim values have different amount of precision lost.

Henry
 
Henry Wong
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Actually, this one may be a more interesting example, since the error is much greater ....



Henry
 
Rob Spoor
Sheriff
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Henry Wong wrote:

There is no such type of problem of rounding because " + " operator will do automatic widening according to its operands and convert both in the bigger one.




I believe Rob was referring to precision lost, due to rounding during the operation...


I most certainly was

And that last example is quite terrifying.
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Rob Prime wrote:
And that last example is quite terrifying.



Numerical computing weenies such as myself know all about this kind of thing. Read about the Kahan summation algorithm to see the lengths that you sometimes need to go to!
 
Rob Spoor
Sheriff
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I knew there could be rounding errors, but all examples I've seen so far have had errors of less than 0.0001.
It makes sense that in your example the value of c is virtually ignored, but I would never have thought up such an example.
 
tushar panda
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Henry Wong wrote:

Believe it or not, this will produce slightly different results.... and the reason is because different interim values have different amount of precision lost.

Henry




what is precision lost
 
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

tushar panda wrote:

Henry Wong wrote:

Believe it or not, this will produce slightly different results.... and the reason is because different interim values have different amount of precision lost.

Henry




what is precision lost



Refer to the post Ernest made with a link to the Kahan algorithm. It explains a lot.
 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hmm, the problem seems like it would be more basic than that.. The question just looks to me like it owuld be something simpler. Have you learned about what Ernest linked to? or is this a more basic java question?
 
To get a wish, you need a genie. To get a genie, you need a lamp. To get a lamp, you need a tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic