Problems with removing last digit of floating point number - c++

I am making a console based calculator application. The application processes user keypresses to perform its operations. Integral inputs work fine; however, I am facing problems writing the code for the case where the user presses a backspace to erase a decimal number.
The code that I wrote to erase decimal spaces is as follows:
decimalcount--; // number of decimal places is subtracted by 1
lnum -= fmod (lnum, pow (10, -decimalcount + 1)); // subtraction
cout << setprecision (decimalcount) << lnum << endl; // display the code
However, for certain numbers like 12.00400679, the values are being improperly subtracted:
12.00400679
12.00400670
12.0040060
12.004000
12.00400
12.0040
12.000
11.90
11.0
10
The full source of the program is as below:
#include <iostream>
#include <iomanip>
#include <cmath>
#include <conio.h>
using namespace std;
int sgn (double x)
{
if (x < 0)
{
return -1;
}
return 1;
}
int main ()
{
cout.setf (ios::fixed);
double lnum = 0, expr = 0;
int decimalcount = 0;
char ch, op;
while (true)
{
ch = _getch ();
if (isdigit (ch))
{
if (! decimalcount)
{
if (sgn (lnum) == sgn (lnum * 10 + sgn (lnum) * (ch - 48)))
{
lnum = lnum * 10 + sgn(lnum) * (ch - 48);
cout << setprecision (0) << lnum << endl;
}
}
else
{
if (decimalcount < 9)
{
lnum += sgn (lnum) * (ch - 48) * pow (10, -decimalcount);
cout << setprecision (decimalcount) << lnum << endl;
decimalcount++;
}
}
}
else if (ch == '\b')
{
if (! decimalcount)
{
lnum -= fmod (lnum, 10);
lnum /= 10;
cout << setprecision (0) << lnum << endl;
}
// This is where I am having problems
else
{
decimalcount--;
lnum -= fmod (lnum, pow (10, -decimalcount + 1));
cout << setprecision (decimalcount) << lnum << endl;
}
}
else if (ch == '.')
{
if (! decimalcount)
{
decimalcount = 1;
cout << setprecision (decimalcount) << lnum << endl;
}
}
else if (ch == 'x')
{
return 0;
}
}
}
Can anyone show me where I am doing it wrong?
Thanks in advance,

You're converting too early. During input, you should keep the
input as a string, and only convert to your internal format when
the input is finished, and you're ready to do the calculations.
But you seem to be doing things the hard way. Why not just use
std::getline on std::cin, and parse the text you receive?
That way, the system handles things like back space, and you
don't have to. If for some reason this isn't acceptable, you
should still factor the input out into a separate function,
which does more or less the same thing.

It is because those values cannot be exactly represented by a floating point number. Its worth reading up on how floating point is represented in memory.
If you require exact decimal places, you may be better off using fixed-point arithmetic. However, implementing trig functions etc may be annoying unless you convert to floating point for such operations.

You should avoid performing arithmetic operations on the number, as that may alter the significant digits you want to keep. Instead, you can convert it to a string and truncate it and then convert back when you need the actual arithmetic value. Examples:
double d;
// ...
std::ostringstream sstream;
sstream << d;
std::string str = sstream.str();
Or:
char str[10];
snprintf(str, 10, "%g", d);
But in general, I'd go with James Kanze's answer and store all input as strings and only convert to numeric values at the end.

Related

Roman Numeral to Integer Conversion Problem

I am so close to solving this roman numeral to integer problem. However, in my if statement for when character equals M, I am getting an error thrown when declaring my previous variable when the input is MCMXCIV for example. Because there is nothing before M, it is throwing an out-of-bounds error. How can I fix this?
#include <iostream>
#include <iomanip>
#include <cmath>
#include <string>
using namespace std;
//Character to search and add to the integer
char character;
//The integer value that is needed to add up and output the corresponding value
int integer = 0;
//One of the test runs and what will be needed for every special case
int main()
{
//Roman numeral given
string input;
//Prompt for user to enter the roman numeral integer
cout << "Enter the roman numeral you want to convert into a number: " << endl;
cin >> input;
cout << input << " is what you are wanting to convert." << endl;
//Read through the string that is being inputted then assign values to the overall integer
for (int i = 0; i < input.length(); i++)
{
character = input.at(i);
cout << "This is the character that is being read right now: " << character << endl;
//Arithmitic for when the character is found and the corresponding value needs to be added
if(character == 'I')
{
integer+=1;
cout << "Integer value now: " << integer << endl;
}
else if(character == 'V')
{
char previous = input.at(i-1);
integer+=5;
if(character == 'V' && previous == 'I')
{
integer = integer - 2;
}
cout << "Integer value now: " << integer << endl;
}
else if(character == 'X')
{
integer+=10;
cout << "Integer value now: " << integer << endl;
}
else if(character == 'L')
{
integer+=50;
cout << "Integer value now: " << integer << endl;
}
else if(character == 'C')
{
integer+=100;
cout << "Integer value now: " << integer << endl;
}
else if(character == 'D')
{
integer+=500;
cout << "Integer value now: " << integer << endl;
}
else if(character == 'M')
{
char previous = input.at(i-1);
integer+=1000;
if(character == 'M' && previous == 'C')
{
integer -= 200;
}
cout << "Integer value now: " << integer << endl;
}
}
cout << "The integer value is: " << integer << endl;
}
First of all: Good that you used the at() function. So you could detect the "out of bounds" problem.
in char previous = input.at(i - 1);, variable "i" could be 0 and you then try to access array element "-1", which is of course out of bounds for your use case.
So, you need an additional check, if "i" greater then 0, before subtracting.
But in general, your approach is too complicated. You can make your life easier, by analyzing or reading, how roman numerals are defined. Look for example here. And please read especially about the "subtractive notation".
You already noticed that but, unfortunately, your implementation is not always following that rule. You made the check only for "M" and "V". But basically, you need to do that for all literals (except "I").
You can boil down this to the rule:
If a literal before a following literal is less, then use the subtrative form. Or, even better, you can read from right to left and finally say:
"If the current literal is less than the follwoing, then use the subtractive form."
And what is the subtractive form? We can simply add the negative number. Example, using number 94 which is "XCIV". We start summing up from the right:
Start. Begin from right. Initialize sum with rightmost value: Looking at 'V': sum = 5
Next: Read 'I'. Check, if this is less than the following literal 'V'. Yes, it is. So, use subtractive form. Add the negative. Now sum = sum + (-1), sum = 4
Now: Read 'C'. Check, if this is less than the following literal 'I'. No, it is not. So, simply add the positive value. Now sum = sum + 100, sum = 104
Next: Read 'X'. Check, if this is less than the following literal 'C' . Yes, it is. So, use subtractive form. Add the negative. Now sum = sum + (-10), sum = 94
So, this is now a very simply algorithm. We will convert a roman literal (one letter) to a integer and then build a sum with positive or negative values.
One of many many potential implementations could look like this:
#include <iostream>
#include <string>
int convert(char romanLiteral) {
switch (romanLiteral) {
case 'I':
return 1;
case 'V':
return 5;
case 'X':
return 10;
case 'L':
return 50;
case 'C':
return 100;
case 'D':
return 500;
case 'M':
return 1000;
default:
return 0;
}
}
int romanLiteralStringToInteger(const std::string& romanLiteralString) {
// Sanity check:
if (romanLiteralString.empty()) return 0;
// Get length of input string
int lengthOfRomanLiteralString = static_cast<int>(romanLiteralString.length());
// Initialize sum with rightmost value
int sum = convert(romanLiteralString[lengthOfRomanLiteralString-1]);
// Now iterate over the string form right to left
for (int i = lengthOfRomanLiteralString - 2; i >= 0; --i) {
// Check if this literal is less than the following
if (convert(romanLiteralString[i]) < convert(romanLiteralString[i+1]))
sum -= convert(romanLiteralString[i]);
else
sum += convert(romanLiteralString[i]);
}
return sum;
}
int main() {
std::string romanNumber = "XCIV";
std::cout << romanNumber << " --> " << romanLiteralStringToInteger(romanNumber) << '\n';
}
In C++ you would probably use associative containers like std::map or std::unordered_map for converting one literal to a number. And maybe a ternary operator, instead of an if.
Then the problem could be implemented like the following:
#include <iostream>
#include <string>
#include <unordered_map>
int romanLiteralStringToInteger(const std::string& romanLiteralString) {
if (romanLiteralString.empty()) return 0;
std::unordered_map<char, int> T = { { 'I' , 1 }, { 'V' , 5 }, { 'X' , 10 }, { 'L' , 50 }, { 'C' , 100 }, { 'D' , 500 }, { 'M' , 1000 } };
int sum = T[romanLiteralString.back()];
for (int i = romanLiteralString.length() - 2; i >= 0; --i)
sum += (T[romanLiteralString[i]] < T[romanLiteralString[i + 1]] ? -T[romanLiteralString[i]] : T[romanLiteralString[i]]);
return sum;
}
int main() {
std::string romanNumber = "XCIV";
std::cout << romanNumber << " --> " << romanLiteralStringToInteger(romanNumber) << '\n';
}
And the hardcore solution with a stateful lambda.
#include <iostream>
#include <string>
#include <unordered_map>
#include <numeric>
#include <iterator>
std::unordered_map<char, int> ARTI{{'I',1 },{'V',5 },{'X',10 },{'L',50 },{'C',100},{'D',500 },{'M',1000 }};
int main() {
std::string romanNumber = "XCIV";
std::cout << std::accumulate(std::next(romanNumber.rbegin()), romanNumber.rend(), ARTI[romanNumber.back()], [&, next = ARTI[romanNumber.back()]](const int s, const char c) mutable {
int sum = s + (ARTI[c] < next ? -ARTI[c] : ARTI[c]); next = ARTI[c]; return sum; });
}

I wrote code to convert decimal fraction number to its binary equivalent. It compiles fine but when executed hangs

I wrote code to convert decimal fraction number to its binary equivalent. It compiles fine but when executed hangs. The code here prints only first four digits of the binary conversion and if the number if with more than 4 digits, it shows '...' after it. On execution it hangs. Help!
#include <iostream>
using namespace std;
int main()
{
int i, x[10];
float num;
cout << "**PROGRAM TO CONVERT DECIMAL FRACTION INTO ITS EQUIVALENT BINARY**\n";
cout << "Enter a fraction in between 0 to 1 for conversion: ";
cin >> num;
if (num>=0 && num<=1)
{
i=1;
while (num!=1.000)
{
num*=2;
x[i]=num;
num=num-x[i];
i++;
}
if (i>4)
{
cout << "The binary equivalent is 0.";
for (i=1;i<=4;i++)
{
cout << x[i];
}
cout << "...";
}
else
{
cout << "The binary equivalent is 0.";
for (i=1;i<=4;i++)
{
cout << x[i];
}
}
}
else
{
cout << "The number entered is out of range.";
}
return 0;
}
The first obstacle is the infinite while loop:
Assuming input num=0.5
after first iteration, i=1, x[0]=1, num=0.0
after second iteration, i=2, x[1]=0, num=0.0
Continue forever, i=..., x[i-]1=0, num=0.0
With nothing to break the loop.
while (num!=1.000)
{
num*=2;
x[i]=num;
num=num-x[i];
i++;
}
To fix, consider few changes. There might be other issues.
put a limit on the while loop (i<10 should be a good condition, as this is the size of the x array), or i=4, as this is the maximum output.
The break condition for the while loop should probably be 'num != 0', or even better (num > 1e-7, or other small value).
float has 23 bit in mantissa, maybe it is because you are assign x[i] with i greater than 9.
try this:
//stop when you get four bits
while (i< 5)
Original code has several issues:
1 For input num=.5 and similar (really for all values) cycle never ends (dash-o suggested fix ideas)
2 array x[10] is overflowed with undefined behavior (Edney)
3 nitpicking: 1 is not a “fraction” and better check for a range 0 <= num < 1 instead of 0 <= num <= 1(see also OP printing code; 1 could be added); we could use x[4] with 0<=i <=3
4 string could also be used (PaulMcKenzie). Really “>>” uses string processing for parsing and calculating binary equivalent from which by multiplying by 2 (left shit) and truncation fractional part the code calculates target bits. Both approaches give correct identical results; implementing by string we need to add internal to operator “>>” implementation code to parsing valid formats for floats (decimals) such as 3.14e-1, .2718, 1e-1, etc.
This fix follows OP:
#include <iostream>
using namespace std;
int main()
{
int i, x[5];
float num;
cout << "**PROGRAM TO CONVERT DECIMAL FRACTION INTO ITS EQUIVALENT BINARY**\n";
cout << "Enter a fraction in between 0 to 1 for conversion: ";
cin >> num;
if (num>=0 && num<1)
{
i=1;
while (i<=4)
{
num*=2;
x[i]=num;
num=num-x[i];
i++;
}
cout << "The binary equivalent is 0.";
for (i=1;i<=4;i++)
{
cout << x[i];
}
if (num>0)
cout << "...";
}
else
{
cout << "The number entered is out of range.";
}
return 0;
}
This code is without cycles (they are in code implementing “>>”, bitset):
#include <iostream>
#include <bitset>
using namespace std;
int main () {
const int digits = 4;
int fraction;
float num;
cout << "**PROGRAM TO CONVERT DECIMAL FRACTION INTO ITS EQUIVALENT BINARY**\n";
cout << "Enter a fraction in between 0 to 1 for conversion: ";
cin >> num;
if (num >= 0 && num < 1) {
fraction = num = num * pow (2, digits);
cout << "The binary equivalent is 0.";
cout << bitset<digits> (fraction);
if (num - fraction > 0) cout << "...";
}
else cout << "The number entered is out of range.";
}

c++ Converting octal fraction to decimal fraction?

I'm currently working on a program that is meant to take an octal fraction as an input and convert it to a decimal fraction. So far, I have the part of code that will convert the portion before the decimal point to decimal, just not the floating points after the decimal point. I was trying to use modulus, but was unsuccessful because of my variable being a float.
Is there a way to convert the remaining numbers after the decimal point to decimal from octal? I have posted my code below. Any help is appreciated. Thanks!
int main()
{
float num;
int rem = 0;;
int dec = 0;
int i = 0;
cout << "Please enter a number with a decimal point: ";
cin >> num;
double ohalf = num - (int)num;
int half = num;
while (half != 0)
{
rem = half % 10;
half /= 10; //Converts first have to decimal
dec += rem *= pow(8, i);
i++;
}
cout << dec;
i = -1;
while (ohalf != 0)
{
rem = ohalf *pow(8, i); //Converts second half to decimal. *This is where I am stuck*
i--;
}
cout << rem;
_getch();
return 0;
}
Going with the idea that one can remove the decimal point just by multiplying with the base often enough:
"123.456" in base 16
=> BASE16("1234.56")/16
=> BASE16("12345.6")/(16*16)
=> BASE16("123456")/(16*16*16)
or
"123.456" in base 8
=> BASE8("1234.56")/8
=> BASE8("12345.6")/(8*8)
=> BASE8("123456")/(8*8*8)
So all we need to know is the number of places behind the decimal point.
Then we can remove it and use std::stoi to convert the remaining string in the wanted base.
As a last step we need divide again through base^number_of_places_after_decimal.
Putting everything together you get something like this:
#include <iostream>
#include <string>
#include <cmath>
using std::cout;
using std::cin;
using std::string;
int main()
{
int base = 8;
string value;
cout << "Please enter a number with a decimal point: ";
cin >> value;
size_t ppos = value.find('.');
if (ppos != string::npos) {
value.replace(ppos,1,"");
} else {
ppos = value.size();
}
size_t mpos = 0;
double dValue = (double)std::stoi(value, &mpos, base);
if (mpos >= ppos)
{
dValue /= std::pow(base, (mpos - ppos));
}
std::cout << dValue << '\n';
return 0;
}
If you're confident that both the integer and fractional parts of your floating-point value won't overflow the range of long long int, you could parse both parts separately with std::stoll(), then divide the fractional part by the appropriate power of 8:
#include <cmath>
#include <stdexcept>
#include <string>
double parse_octal_fraction(const std::string s)
{
std::size_t dotpos;
auto whole = std::stoll(s, &dotpos, 8);
if (dotpos+1 >= s.length())
// no fractional part
return whole;
std::size_t fract_digits;
auto frac = std::stoll(s.substr(dotpos+1), &fract_digits, 8);
if (s.find_first_not_of("01234567", dotpos+1) != std::string::npos)
throw std::invalid_argument("parse_octal_fraction");
return whole + frac / std::pow(8, fract_digits);
}
#include <iostream>
int main()
{
for (auto input: { "10", "0", "1.", "0.4", "0.04", "1.04", "1.04 ", "1. 04"})
try {
std::cout << input << " (octal) == " << parse_octal_fraction(input) << " (decimal)" << std::endl;
} catch (const std::invalid_argument e) {
std::cerr << "invalid input: " << e.what() << " " << input << std::endl;
}
}
The test inputs shown give this output:
10 (octal) == 8 (decimal)
0 (octal) == 0 (decimal)
1. (octal) == 1 (decimal)
0.4 (octal) == 0.5 (decimal)
0.04 (octal) == 0.0625 (decimal)
1.04 (octal) == 1.0625 (decimal)
invalid input: parse_octal_fraction 1.04
invalid input: parse_octal_fraction 1. 04
in your code
while (ohalf != 0)
{
rem = ohalf *pow(8, i); //Converts second half to decimal. *This is where I am stuck*
i--;
}
ohalf will never be equal to zero so It may have lead to infinite loop

Program in c++ tell if a number is integer or float

I used function overload to check if an input number is integer or float. However I get this following error:
error: call of overloaded 'retNr(double)' is ambiguous|
#include <iostream>
using namespace std;
void retNr(int x)
{
cout << "The entered number is an integer. " << endl;
}
void retNr(float x)
{
cout << "The entered number is a float. " << endl;
}
int main()
{
cout << "Please enter a number: " << endl;
cin >> nr;
retNr(nr);
return 0;
}
Read from cin into a string and then check the string for the presence of a decimal point. If there is a decimal point, call atof() on the string to convert it to a float, otherwise call atoi() to convert it to an integer.
Make some small change in:
void retNr(double x)
{
cout << "The entered number is a double. " << endl;
}
Remember to declare your nr variable.
double d = 1.0;
int i = 1;
retNr(d);
retNr(i);
You will have to initialize nr first.
Then you can use integer read & check it with a float if there is dot, ie ch=='.'
Thus, your program will be like this:
#include <iostream>
using namespace std;
int main()
{
int nr = 0; char ch;
cout << "Please enter a number: " << endl;
cin >> nr;
cin.get(ch);
if(ch=='.')
{
cout << "The entered number is a float. " << endl;
}
else
{
cout << "The entered number is an integer. " << endl;
}
return 0;
}
It's not too clear what you're asking for. If you really want
to know whether a number is an integer or not, then use modf
on it:
bool
isInt( double d )
{
double dummy;
return modf( d, &dummy ) == 0.0;
}
If you're reading a number, then read it as a double, and then
use the above.
If you want to trigger off the format of the input (i.e.
"10.0" will be treated as a floating point, even though it is
an integer), then read the input as a string, then try to
convert it to int; if this eats all of the input, then it was
entered as an int (no decimal or exponent), otherwise, try the
same thing treating it as a double:
std::string entry;
std::cin >> entry;
char const* end;
long i = strtol( entry.c_str(), &end, 10 );
if ( *end == '\0' ) {
// entry was entered in integral format...
} else {
double d = strtod( entry.c_str(), &end );
if ( *end == '\0' ) {
// entry was entered in floating point format...
} else {
// entry wasn't a number...
}
}
I'd advise against this, however; it will only confuse your
users if 0 isn't 0.0.
You may use abs() function for this issue.
#include<stdio.h>
#include<math.h>
int main()
{
double input;
scanf("%lf",&input);
int absulate = abs(input);
printf( (input==absulate)? "It is integer\n" : "It is float");
return 0;
}
float num = 7;
int n = (int)num;
float n1 = (float)n;
if(num == n1)
{
cout << "Integer\n";
}
else
{
cout << "Not Integer\n";
}
The question is wrong in its essence: A number it is not a float or an integer, but could be represented as a float or as an integer ( of course certain representation has some limitations )
So if I wrote '10' why should I say this is an integer? Could be a float too! Just if I want to use it as a float I would represent it as a float.

Large binaries add

this is my first question, so I hope I will not break any of the given rules here on forum. I would like to ask you for help. Im really programming noob, but for homework I have to make a programm in C++ which will add 2 binary numbers. I was able to make it throught converting to a decimal and adding them. I did it bcs I already had some parts for it in my PC. My question is, everything is working fine unless I enter really big binary numbers. Changing data types make difference in results when our school program checks the code. Im not sure waht to change exactly. Thank you in advance. It looks like proble occure when decimal number with "e" has to be converted-
#include <iostream>
#include <cmath>
#include <string>
#include <vector>
using namespace std;
int main ()
{
int k = 0;
int l = 0;
int i = 0;
int j = 0;
double number = 0;
double numberb = 0;
long dec;
string input;
string inputb;
cout << "Enter two binary numbers:" << endl;
cin >> input >> inputb;
if(cin.fail ())
{cout << "Wrong input." << endl;
return 0;
}
for (i = input.length() - 1; i>=0; i-- )
{
if (input[i] != '1' && input[i] != '0')
{
cout << "Wrong input." << endl;
return 0;
}
if (input[i] == '1')
{
number += pow((double)2,(int)j);
}
j++;
}
for (k = inputb.length() - 1; k>=0; k-- )
{
if (inputb[k] != '1' && inputb[k] != '0')
{
cout << "Wrong input." << endl;
return 0;
}
if (inputb[k] == '1')
{
numberb += pow((double)2,(int)l);
}
l++;
}
dec = number+numberb;
vector <double> bin_vector;
long bin_num;
while ( dec >= 1 )
{
bin_num = dec % 2;
dec /= 2;
bin_vector.push_back(bin_num);
}
cout << "Soucet: ";
for ( int i = (double) bin_vector.size() - 1; i >= 0; i-- )
cout << bin_vector[i] << "";
cout << endl;
return 0;
}
Probably your teacher told you that double numbers are great for large numbers. They are, but they have a big disadvantage: They cannot represent large numbers exactly, since they (roughly speaking) only store the first few digits of the number, together with the position of the decimal point, like your pocket calculator shows (for example, 123456E13).
So you should not use double numbers here at all, since you need precise results, no matter how large your numbers are. A better idea is process both strings simultaneously and store the result digit by digit in another string, called result.
By the way: Since double numbers can usually store 53 binary digits precisely, it's good that your tested the program with even more digits.