Why pow(10,2)=99 & pow(10,3)=1000? [duplicate] - c++

This question already has answers here:
Is floating point math broken?
(31 answers)
Closed 3 years ago.
I use codeblock for compilation.
code:
int main(){
int l=pow(10,2);
cout<<l;
return 0;
}
OUTPUT-99
int main(){
int l=pow(10,3);
cout<<l;
return 0;
}
OUTPUT-1000

In short - because of integer truncation.
The function pow operates on two floating-point values and returns a floating-point value. When working on integers, the result of pow(10,2) might be stored as 99.9999999, or 100.0000000001. Due to integer truncation, 99.9999999 gets truncated down to 99, and 100.0000000001 gets truncated down to 100.
Same goes for pow(10,3), with 999.9999999, or 1000.0000000001 - which will be truncated to 999 or 1000.
You can read more about the pow function here.

Related

c++11 double precision [duplicate]

This question already has answers here:
Is floating point math broken?
(31 answers)
Closed 9 months ago.
Doing the following calculation:
double result = 58/10;
Will result: 5.7999999999999998
I know c++ will round it on further calculations or printing, but I need to compare output with where rounding doesn't occur so I can't have rounding happen because that may just lead to differences in other places in the output.
Is there any way to improve the precision of such calculations and having 58/10 result 5.8?
5.8 cannot be exactly represented in floating point. If you are working with 2 digits precision after the decimal point, for example if you need to store monetary values, store it as "580" (e.g., 580 cents) and do your own formatting when printing.
printf("%d.%02d", n / 100, n % 100);
Alternatively store the decimal as a fraction:
struct Fraction {
int numerator;
int denominator;
};
int main() {
Fraction f(29, 5);
}

Comparing float to a decimal point number [duplicate]

This question already has answers here:
Is floating point math broken?
(31 answers)
Why are floating point numbers inaccurate?
(5 answers)
Floating point comparison `a != 0.7` [duplicate]
(3 answers)
Closed 1 year ago.
This is a simple code which should have printed "No" , but its printing "Yes". It works fine if I use double instead of float Or compare digits upto 1 decimal place. I am using online C++14(gcc+14) compiler here. Why is this happening?
#include <iostream>
using namespace std;
int main() {
float s= 10.11;
cout<<(( s < 10.11) ? "Yes": "No")<<endl;
return 0;
}

Why does "double i = 1/12;" yields to i = 0? [duplicate]

This question already has answers here:
Why does dividing two int not yield the right value when assigned to double?
(10 answers)
Closed 5 years ago.
I think the title says everything. I want to define a variable i as the fraction 1/12. However, i is 0.
double i = 1/12;
std::cout << i; // Output: 0
Or, more specific, I want to calculate a power of something:
im_ = std::pow((1 + i), (1/12)) - 1;
However, the compile evaluates (1/12) as 0 and thus the result is wrong.
Simple because 1/12 is evaluated as integer math, not floating point math.
1/12 becomes 0 because integer math does not take into account the decimal fractions.
To get the expected result you will need to write down the numbers as a floating point literal, like this: 1.0/12.0.
More details can be found here: Why can't I return a double from two ints being divided

Floor vs int cast difference [duplicate]

This question already has answers here:
Why does floating-point arithmetic not give exact results when adding decimal fractions?
(31 answers)
Closed 6 years ago.
I have a weird problem. Here's part of my code:
int temp=1100;
int foo=floor(0.03*temp);
int foo1=0.03*temp;
if(foo-foo1){
cout<<foo<<endl;
cout<<foo1<<endl;
}
If 3% of temp = integer then foo differs from foo1 by 1.
For example: 1100*0.03=33. foo=33 foo1=32.
In addition if i write it like that :
int foo=floor(0.03*1100);
int foo1=0.03*1100;
There is no such problem.
Why?
Floating point numbers have trouble rendering decimal places. It is more of an approximation.
2**-6 + 2**-7 + 2**-8 + 2**-9 + 2**-11 + 2**-13 + 2**-14 + 2**-15 + 2**-20 + 2**-22 + 2**-26
= 0.0299999863
Using binary, I drove the accuracy to 26 binary. I got close to 0.03 but not quite. In my example I chose to be under 0.03 but I could have gone a little above (i.e. 0.03000001. I don't think it is possible to represent 0.03 perfectly in floating point notation.
Multiplying 0.03 by any number produces yet another approximation. Casting to type int will cut out everything after the decimal place. I assume the implementation of floor is more elegant. Your compiler probably choose a floating point value of 32.99999 so and int would make 32.

My C++ code not showing floating point value [duplicate]

This question already has answers here:
Why does this division result in zero?
(6 answers)
Why do my division results appear to be off?
(2 answers)
Closed 9 years ago.
My code is not showing exact float value means number after point is not displayed by the code
I am using Turbo C for sum up the series 1 + 1/3 + 1/5 + 1/7 + 1/9+….. Up to N terms
#include<iostream.h>
#include<conio.h>
void main()
{
int k=0;
int m=0;
int s=1;
clrscr();
cout<<"Enter the limit of the series: ";
cin>>m;
for(int j=1;j<=m;j=j+2)
{
m=1/j;
s+=m;
}
cout<<"Sum of the given series is: "<<s;
getch();
}
You're using int which only displays Integer (ie whole number) values. It truncates any decimal places because it assumes you don't want them. Try using float or double instead.
Integer division will not give you anything other than integer results.
You need to:
Change s to a float or double.
change m to a float or double.
Change 1 in 1/j to 1.0f or 1.0 (for float and double respectively).
Now, you probably also want to use a different variable than m for your input and for-loop limit variable, so that you don't stop too quickly once the calculation starts.
Use the type double for variables m and s.
Here are the variables you declared in your code.
int k=0;
int m=0;
int s=1;
Where exactly do you think you have a floating point number?
(which would have to be of type float or double)
You do know that int means integer, right?
(eg. Not a floating point number)