Ask Your Question
0

when float number is above 0.5 and when is below 0.5?

asked 2021-05-25 20:16:46 +0100

Miroslaw gravatar image

updated 2022-04-14 10:54:29 +0100

FrédéricC gravatar image

Dear Collegues,

i have read a lot of papers and nowhere I can find any solution or solutions to verified when float number is above 0.5 and when below 0.5

example:

number 0.2 -> below 0.5
number 0.55 -> above 0.5

what I have : to verified in algorithm : number 1.345 , and I;d like to know how to check that is more than 1.5 or les than 1.5? I cant check by if 1.345 > 1.5 then print("above") :)

it is any solution algoritms how compute calculate it?

Now i will describe the problem: I have a number which I divide by 2: for example

number =  1699
1 849.5
2 424.75
3 212.375
4 106.1875
5 53.09375
6 26.546875
7 13.2734375
8 6.63671875
9 3.318359375
10 1.6591796875

the last line - "10" there is 1.6591796875 -> it tranlated -> 1 * 2^10 + 2^10 * 0.6591796875 = 1699

and know -> I only know that there is value x which is 1.6591796875 , but I don't know this exactly number , but only know there is 1 plus rest. so I can substract from 1.6591796875 - 1 = to get rest : 0.6591796875 -> and here how to check it is more than 0.5 or less than 0.5?

edit retag flag offensive close merge delete

1 Answer

Sort by » oldest newest most voted
1

answered 2021-05-25 20:38:14 +0100

tmonteil gravatar image

updated 2021-05-25 20:41:43 +0100

Short anwser :

sage: a = 1699./2^10 - 1
sage: a > 0.5
True

As 0.6591796875 is very far from 0.5, there is no issue here. But if the number are closer to eachother, and could be equal, you have to use more robust methods than raw floating-point nunbers. The right choice of numbers depends on your application, so you should provide more details.

To learn more about the various representations of real numbers, let me suggest to read the following worksheet : https://mathexp2018.metelu.net/worksh...

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2021-05-25 20:16:46 +0100

Seen: 523 times

Last updated: May 25 '21