This question already has answers here:
strange output in comparison of float with float literal
(8 answers)
Closed 4 years ago.
#include<iostream>
using namespace std;
int main(){
float a=1.11;
if(a==1.11){cout<<"yes";} else {cout<<"no";}
return 0;
}
Result:
no
Process exited after 0.2579 seconds with return value 0
Press any key to continue . . .
Captured Result here
Type 1.11f
#include<iostream>
using namespace std;
int main(){
float a=1.11f;
if(a==1.11f){cout<<"yes";} else {cout<<"no";} //here not
return 0;
}
Related
This question already has answers here:
Structure padding and packing
(11 answers)
Why isn't sizeof for a struct equal to the sum of sizeof of each member?
(13 answers)
Closed 18 days ago.
# include <stdio.h>
# include <iostream>
using namespace std;
struct complex{
int real;
int img;
char x[5];
int add(struct complex c2){
real += c2.real;
img += c2.img;
}
void display(){
printf("%d + %d i",real,img);
}
};
int main(){
struct complex c1={10,15
};
struct complex c2 = {12,15
};
cout<<sizeof(c1)<<endl;
c1.add(c2);
c1.display();
}
I am getting 16 as its size in sizeof();how does it work ,Can you guys please help me?,I am getting 16 as its size for character array from size 5 to 8,from 9 it is 20.
This question already has answers here:
Why doesn't a negative number modulo a vector size give a negative number? [duplicate]
(5 answers)
Closed 1 year ago.
This code snippet:
#include <iostream>
#include <cstddef>
int main()
{
int a{-4};
std::size_t b{3};
std::cout << a % b;
return 0;
}
prints
0
while this code snippet:
#include <iostream>
int main()
{
int a{-4};
int b{3};
std::cout << a % b;
return 0;
}
prints
-1
So, why does the first code snippet returns 0? Why does changing b from std::size_t to int print the right result?
Oh, it's because a implicitly converted into std::size_t becuase b is std::size_t, which is 4294967292(on my machine), and 4294967292 % 3 == 0
This question already has answers here:
Checking for underflow/overflow in C++?
(5 answers)
Saturating subtract/add for unsigned bytes
(11 answers)
Closed 3 years ago.
How can it be possible to determine if subtracting n from an unsigned int will roll over to a negative value, considering that casting to a signed int can result in a negative value already?
Example
#include <iostream>
using namespace std;
int main(){
unsigned int i = 2147483647*2;
if((int)i - 1 < 0){
cout << "rolled over";
}
else {
i = 0;
}
return 0;
}
In order to check if subtracting from an unsigned int would roll over, you could cast it to an int first. However, if the unsigned int is > the max int value, you will already end up negative. So how can I prevent an unsigned int from rolling over?
This question already has answers here:
Why does pow(n,2) return 24 when n=5, with my compiler and OS?
(4 answers)
Closed 6 years ago.
I just tried three pieces of code:
#include<cstdio>
#include<cmath>
#include<cstring>
int main()
{
int a = 3;
int b = pow(10,a);
printf("%d",b);
return 0;
}
//Output:1000
#include<cstdio>
#include<cmath>
#include<cstring>
int main()
{
int a = 3;
int b = pow(10,a-1);
printf("%d",b);
return 0;
}
//Output:99
#include<cstdio>
#include<cmath>
#include<cstring>
int main()
{
int a = 3;
int b = pow(10,a-2);
printf("%d",b);
return 0;
}
//Output:10
I would like to know why the second block of code will output 99, is it because of floating point precision? Or is it because I should use float numbers in the pow function?(Such as 10.0)
I'm usually confused about the accuracy of C++, I will be grateful for your help.
For integer exponents the following template can be very handy:
template <typename T> inline constexpr T pow( T base, int exponent )
{
return (exponent == 0) ? static_cast<T>(1.0) : ( (exponent>0) ? base*pow(base, exponent-1) : pow( static_cast<T>(1.0)/base, -exponent ) );
}
If you plan to use it with a C++ standard prior C++11, just remove the constexpr keyword.
Conversion of a floating-point value to an integer is done by truncation — you get the next integer closest to zero. If pow is imprecise and too low, then truncation will exacerbate it.
lround(pow(10,2)) might be more appropriate.
This question already has answers here:
C++: How to round a double to an int? [duplicate]
(5 answers)
round() for float in C++
(23 answers)
Closed 8 years ago.
I am stuck in problem where the double number is not getting properly converted to integer.
In this case->
int x=1000;
double cuberoot=pow(x,(1/(double)3));
int a=cuberoot;
cout<<"cuberoot="<<cuberoot<<endl;
cout<<"a="<<a<<endl;
Output:
cuberoot=10
a=9
Why here a=9 and not 10?
Any solution to this problem??
Also I don't want to round the value..if a=3.67 then it should be converted to 3 only
and not 4.
Because the cuberoot is very close to 10 but not quite 10. std::cout truncates and rounds the number to 10, but a double to integer conversion will strip the decimal which is why a = 9. To solve this problem, you can use std::round():
int a=round(cuberoot);
Try this and see why!
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
int main(){
int x = 1000;
double cube = pow(x, 1.0/3.0);
int a = cube;
cout<<"cube="<< fixed << setprecision(16) << cube<<endl;
cout<<"a="<<a<<endl;
}