+ 1

How do I detect floats?

Is there any way of detecting floats in an if statement (or detecting floats in general? For example: float x = 32 if (x == float) {cout << "true"} else {cout << "false"} Output: false float x = 68.7 if (x == float) {cout << "true"} else {cout << "false"} Output: true

28th May 2022, 6:28 PM
Yugix
Yugix - avatar
2 odpowiedzi
+ 4
Many programming languages feature an operator such as "typeof", which returns the type of the variable given. For example, in javascript: let x = 5; console.log(typeof x); // "number" It appears that you are using C++, which does not have a typeof operator but instead a built-in function called "typeid". In your case, the if statement would then become: if (typeid(x) == typeid(float))
28th May 2022, 6:41 PM
Daniel C
Daniel C - avatar
+ 3
Compare the value with its truncated or integer version: string isFloat(float a) { return (a==(int)a)? "False" : "True"; }
28th May 2022, 6:52 PM
Brian
Brian - avatar