## Integer or double 1.0 and 1

I want to read a double number and determine if the input is Integer or double. The problem is that when I enter `1.00`

(which is double) I get the result as integer

double a; cin >> a; if (a == int(a)) cout << "Integer"; else cout << "Double";

Perhaps `std::variant`

is an elegant way to solve your problem. `std::variant<int, double>`

can store both an `int`

or a `double`

. Dependent on what you store the internal type will change.

#include <variant> #include <string> #include <cassert> bool isInt(const std::variant<int, double> &intOrDouble) { try { std::get<double>(intOrDouble); } catch (const std::bad_variant_access&) { return false; } return true; } int main() { std::variant<int, double> intOrDouble; intOrDouble = 12.0; // type will be double -> return will be 1 //intOrDouble = 12; // type will be int -> return will be 0 return isInt(intOrDouble); }

**why a double 1.0 its equal to an int 1?,** Why is not a good idea to compare directly different data types (in this case double - integer) and why I this time I obtain equality ?? share. # identifies the vector type (double, integer, logical, or character) typeof (dbl_var) ## [1] "double" typeof (int_var) ## [1] "integer" Converting Between Integer and Double Values By default, if you read in data that has no decimal points or you create numeric values using the x <- 1:10 method the numeric values will be coded as integer.

You can read into a string and check if it contains the decimal separator. Assuming it is '.', here's an example implementation:

#include <iostream> #include <string> int main() { std::string s; std::cin >> s; std::cout << ((s.find('.') == std::string::npos) ? "integer" : "double") << std::endl; return 0; }

You also have to check for exponents (like `2e-1`

). Here's one way to do it all:

#include <iostream> #include <string> int main() { std::string s; std::cin >> s; if (s.find_first_of(".,eE") == std::string::npos) std::cout << "integer" << std::endl; else std::cout << "double" << std::endl; return 0; }

**what's the difference between writing 1.0 and 1. or 2.0 and 2 ,** When writing C code, "1" and "1.0" create an integer or double constant, but in Matlab both are doubles. If you want to emphasize that a constant is a floating point value, write "1.0" instead of "1" or "1." double dValue = 1.0; double dOneThird = dValue/3; Technically this is what is known as a mixed-mode expression because dValue is a double but 3 is an int . Okay, C++ is not a total idiot — it knows what you want in a case like this, so it converts the 3 to a double and performs floating-point arithmetic.

In your if statement you are casting a to an int. This is just going to truncate the decimal value.

1==1 is always true as is 1.0 == 1

I would suggest looking at this answer: How to validate numeric input C++

**Integer vs. Double � UC Business Analytics R Programming Guide,** create a string of double-precision values dbl_var <- c(1, 2.5, 4.5) dbl_var ## [1] 1.0 2.5 4.5 # placing an L after the values creates a string of integers int_var� A double-precision floating point number that is greater than or equal to 0.0, and less than 1.0.

**Mixing integer and floating point numbers in an arithmetic operation,** All floating point values (float and double) in an arithmetic operation (+, −, *, /) are Java's automatic integer conversion in arithmetic operations: Example 1: void main(String[] args) { short a = 5; int b = 2; double c = 1.0; double d = 5.0;� When writing C code, "1" and "1.0" create an integer or double constant, but in Matlab both are doubles. I recommend to write code with the highest possible clarity to improve the readability: If you want to emphasize that a constant is a floating point value, write "1.0" instead of "1" or "1."

**4.8 — Floating point numbers,** 1. 2. 3. int x{5}; // 5 means integer. double y{5.0}; // 5.0 is a floating point literal (no Use double literals when calculating with doubles (1.0 instead of 1, 24.0� Double precision is not required by the standards (except by the optional annex F of C99, covering IEEE 754 arithmetic), but on most systems, the double type corresponds to double precision. However, on 32-bit x86 with extended precision by default, some compilers may not conform to the C standard and/or the arithmetic may suffer from double

**What does that "f" signify?,** In c a value of 1 is an integer and 1.0 is a double, you use f after a decimal number to indicate that the compiler should treat it as a single� If you read as a double then you really can't tell if the user entered 1.0 or 1. The double doesn't store or retain a difference. – drescherjm Mar 25 '19 at 14:25. 4. A variable of type double is never an integer.

##### Comments

- Read as a string, and then see if the string contains a decimal separator (could be
`','`

as well, depending on locale settings). For conversion and further validation use e.g.`std::stod`

. - The result is not an integer, your check is simply wrong. you declare
`a`

to be a double, so`cin >> a;`

will assign whatever you type to a double, so`a`

will never be an Integer. If you want to check whether the input is formatted like an integer or a floating point type (float, double, long double) and contains e.g. a`.`

like`1.0`

or`.5`

, assign the input to a string and check how the value is formatted. - I already know this method, but what I am asked to do is to read from a double not a string.
- If you read as a double then you really can't tell if the user entered 1.0 or 1. The double doesn't store or retain a difference.
- A variable of type
*double*is never an integer. Presumably that's what a call a double whose fraction is 0. Use this: cplusplus.com/reference/cmath/modf - That's neat but how would you read from standard input without knowing the type you are reading? If the value is already read, what's stopping you from comparing
`typeid`

s? - Exactly, though I did not know it would be that terribly difficult
`c++`

is statically typed. stackoverflow.com/questions/1517582/…- @drescherjm How is this relevant?
- I got the feeling Florian wanted
`c++`

to behave like a dynamically typed language. - My task is to read from a double and thats why I am stuck for several hours