N
N
nait1233212015-11-08 15:42:44
C++ / C#
nait123321, 2015-11-08 15:42:44

How to check the parity of a floating point number?

Hello, how to check the parity of a floating point number in C language?
x%2==0 - doesn't work.

Answer the question

In order to leave comments, you need to log in

6 answer(s)
M
mletov, 2015-11-08
@mletov

Parity in number theory - a characteristic of an integer
(Wikipedia)

P
Puma Thailand, 2015-11-08
@opium

Well, bring to the whole if not fractional

T
Tsiren Naimanov, 2015-11-08
@ImmortalCAT

um
, and if you multiply up to an integer
and x%2==0
, will it work?)
PYSY
can be done like this

var myvar = 124124,2541;
var strmyvar = myvar as string;
var in = strmyvar[strmyvar.Count-1];
bool result = in%2==0;

long code, but it can be shortened :)
just take the last digit and judge its multiplicity by it

O
Oleg Tsilyurik, 2015-11-08
@Olej

A real number cannot be even or odd. A real number is always an approximate value (at least for this reason, its remainder when divided by 2 will always be non-zero).

M
Mrrl, 2015-11-08
@Mrl

double y=x/2;
  bool isEven=(fabs(y-round(y)) < EPS);

A
Aenigma, 2021-12-21
@Aenigma

It follows from the context that we are talking about integers represented in real format. If so, then converting to an integer, as advised above, is not a universal method, since the number can go beyond the bit grid of the integer type. You need to check as follows:
0.5*x==round(0.5*x)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question