Which Data Type?

I am writing a calculator code but I need a primary data type that can store integer numbers and also decimal numbers at the same time. Is there such a thing in C++ if no what can I do.
What's the biggest number you want to be able to store? How much floating-point precision do you require?

An IEEE 64-bit double can store integers up to 253 without losing any integer precision. https://stackoverflow.com/questions/1848700/biggest-integer-that-can-be-stored-in-a-double

Another option is that you store the numbers as strings and then convert them either into integers or doubles as required.
Last edited on
Hello Fresh Coder,

In addition to what Ganado has said it is a good idea to post what code you have written, so everyone can see what you have done and have a better idea of what you are trying to do or need to do.

Also if this is a school assignment it helps to post the instructions that you were given. And I do mean the actual instructions not what you think it means as your idea may not be correct.

Next it helps to know what you do know and/or what you do not know or can not use in a program.

As a reference to posting code, and keep in mind this is just a copy and paste,:

PLEASE ALWAYS USE CODE TAGS (the <> formatting button), to the right of this box, when posting code.

Along with the proper indenting it makes it easier to read your code and also easier to respond to your post.

http://www.cplusplus.com/articles/jEywvCM9/
http://www.cplusplus.com/articles/z13hAqkS/

Hint: You can edit your post, highlight your code and press the <> formatting button. This will not automatically indent your code. That part is up to you.

You can use the preview button at the bottom to see how it looks.

I found the second link to be the most help.



To your question. Choosing a data type really depends on the program. For something simple like num1 + num2; you could use an "int" or "double". These days a "double" is the preferred floating point type. Even then a "float" or "double" may not store the decimal number properly. An example: num = 0.3;. You would think this would store as "0.3", but you are more likely to get "0.29999999999999989 and I have see a float store the decimal part higher than the entered number.

Printing the number to the screen with 1 or 2 decimal place may show the number that you expect, but in a calculation the answer will be slightly off.

Without a better idea of what the program should do or work with it sounds like the "double" may be the best choice. Especially when you get to the divide part.

Andy
what about <complex> ?
It supports everything you need, I believe.

complex<int> i;
complex<double> d;
you can ignore (zero it out!) the imaginary part and just use the real part if you don't need that.

https://en.cppreference.com/w/cpp/numeric/complex
Last edited on
std::complex<int> is a bad idea; it may not work as expected.

The effect of instantiating the template complex for any type other than float, double, or long double is unspecified - IS https://eel.is/c++draft/complex.numbers
This is my code down below if you look where I said int num1 and int num2. They only allow me to store integer types of numbers because of int so is there a way for me to get a data type that can store integer numbers and also decimal numbers.


#include <iostream>
#include <string>
#include <cmath>
using namespace std;

int main()
{
int num1;
int num2;
char oper;
cout << "Put in a Number" << endl;
cin >> num1;

cout << "Put in another Number" << endl;
cin >> num2;

cout << "Type in one of these operators (* / - + ^ %)" << endl;
cin >> oper;
switch (oper)
{
case '+':
cout << num1 + num2 << endl;
break;
case '/':
cout << num1 / num2 << endl;
break;
case '*':
cout << num1 * num2 << endl;
break;

case '-':
cout << num1 - num2 << endl;
break;
case '^':
cout << pow(num1, num2) << endl;
break;
case '%':
cout << num1 % num2 << endl;
break;
default:
cout << "Not Vailid Try Again" << endl;
break;
}

0;
}
Last edited on
Replace int with double.

You must adjust case '%' to call std::fmod:
1
2
3
case '%':
  cout << std::fmod(num1, num2) << endl;
  break;
@mbozzi Thanks a lot I was wondering how to fix that line you mention now it's fixed but what is std::fmod(num1, num2). Like what does it mean?
Hello Fresh Coder,

Have a look at this:
https://en.cppreference.com/w/cpp/numeric/math/fmod

Andy
Topic archived. No new replies allowed.