Math Is Fun Forum

  Discussion about math, puzzles, games and fun.   Useful symbols: ÷ × ½ √ ∞ ≠ ≤ ≥ ≈ ⇒ ± ∈ Δ θ ∴ ∑ ∫ • π ƒ -¹ ² ³ °

You are not logged in.

#1 2022-09-12 04:43:20

Hannibal lecter
Member
Registered: 2016-02-11
Posts: 392

compare between numbers using Boolean expressions

Hi,

if (-0.01 < x - y && x - y < 0.01)
         {
            print : "The numbers are close together");
         }

what is the meaning of -0.01?
and how could we compared between two numbers distance using this short unknown expression?
I mean for example I'll try two close numbers but it didn't work like :
x = 1, y= 0.5
like :
(-0.01 < 1 - 0.5 && 1 - 0.5 < 0.01)
( true && false )
the result is false !!!! so the numbers are not close but they are close it's 1 and 0.5 look 0--0.5--1.5--2-2.5...

so the numbers are not close togather? is that expression wrong? and if it's wrong what is the correct one


Wisdom is a tree which grows in the heart and fruits on the tongue

Offline

#2 2022-09-12 08:48:18

Bob
Administrator
Registered: 2010-06-20
Posts: 10,623

Re: compare between numbers using Boolean expressions

Hi,

This is just a way of saying they're close if the difference is less than 0.01

Say x > y, then close if x-y < 0.01

And if  x < y then close if y - x < 0.01

=> x - y > -0.01

Bob


Children are not defined by school ...........The Fonz
You cannot teach a man anything;  you can only help him find it within himself..........Galileo Galilei
Sometimes I deliberately make mistakes, just to test you!  …………….Bob smile

Offline

#3 2022-10-03 23:50:48

Hannibal lecter
Member
Registered: 2016-02-11
Posts: 392

Re: compare between numbers using Boolean expressions

EPSILON = 1E-14;
r = sqrt(2.0)

if  abs(r * r - 2.0) < EPSILON
output :-
sqrt(2.0) squared is approximately 2.0

this when I use java but when I use Microsoft calculator it said false! the condition would be false not true as you can see in this picture :-

2022-10-04-144854.png


also as you can see in this example :-

double x = 9.999999999999999;
if (Math.abs(x- 10.0) < EPSILON)
{
System.out.println("the number is approximately 10"); 
    }
}
}

when I minus the 9.999999999999999 to 9.99 for example the condition will be false!




in short I just want to solve this question :-

Explain why it is more difficult to compare floating-point numbers than integers.
Write Java code to test whether an integer n equals 10 and whether a floating-point
number x is approximately equal to 10

Last edited by Hannibal lecter (2022-10-04 02:07:26)


Wisdom is a tree which grows in the heart and fruits on the tongue

Offline

#4 2022-10-04 02:52:27

Bob
Administrator
Registered: 2010-06-20
Posts: 10,623

Re: compare between numbers using Boolean expressions

I went to Wolfram-Alpha https://www.wolframalpha.com to compute

Abs(1.41421356*1.41421356 - 2) and got the result 6.7121264 * 10^(-9)

So the expression isn't less than 10^(-14). So FALSE is what I would expect.

Bob


Children are not defined by school ...........The Fonz
You cannot teach a man anything;  you can only help him find it within himself..........Galileo Galilei
Sometimes I deliberately make mistakes, just to test you!  …………….Bob smile

Offline

Board footer

Powered by FluxBB