How to tell if a number is not an integer

You can do this:

int m = floor(sqrt(n) + 0.5); //对n开方并且四舍五入(floor()函数是向下取整)
if(m*m == n) printf("%d是整数", n);

In fact, my first thought is that this

if(sqrt(n)==floor(sqrt(n)))	printf("%d\n",n);

as well as

int m = floor(sqrt(n));
if(m*m == n) printf("%d是整数", n);

These two methods are the same essence, theoretically feasible, but after a large number of calculations, an error is generated, for example, becomes an integer of 1 0.999999999, if taken directly rounding down, it becomes 0.999999999 0 rather than 1. therefore, using this method to reduce rounding errors.

Published 10 original articles · won praise 9 · views 2705

Guess you like

Origin blog.csdn.net/King_Louis_/article/details/104731256