#adding doubles doesnt give the expected outcome

1 messages · Page 1 of 1 (latest)

west glacierBOT
#

Helper please have a look, thanks.

gritty cloud
#

huh

fringe elbow
#

oops i deleted the question

gritty cloud
#

0.1+0.2==0.3

fringe elbow
#

yeah well

#

this

#

.-.

gritty cloud
#

this is false because of how floatting points work

fringe elbow
#

do you know how i can like

#

fix it

gritty cloud
#

they do a lot of rounding

#

so you can't expect them to be extremely precise

fringe elbow
#

hmm

#

thats interesting

#

do i like round it again

#

to like 1 decimal point?

#

does that fix it

gritty cloud
#

so either you don't use floating points, and try to find a way by manipulating ints or something else, or use BigDecimal which has unlimited precision, or, simplest solution, instead of using ==, you use >= and <= with a very small nterval

fringe elbow
#

thanks!

#

ill try that

gritty cloud
#
boolean areDoubleEqual(double a, double b, double gap) {
  return Math.abs(a - b) <= gap;
}
// Usage
if(areDoubleEqual(0.1+0.2, 0.3, 0.00001))
#

@fringe elbow

fringe elbow
#

thank you bro

#

appreciate it

#

lemme just

#

copypaste that :)