How can I compare two cpp_int values like
boost::multipercision::uint256_t x = 100;
boost::multipercision::uint256_t y = 50;
if (x > y){
std::cout << "X is bigger than Y" <<endl;
} else {
std::cout << "Y is bigger than X" <<endl;
}
You have some syntax errors in your code.
boost::multiprecision is not spelled boost::multipercision.
Also, your if-else block should include a check for x == y, since in your code, it'll simply output Y is bigger than X when x and y are the same.
Related
I am new to C++ and have the following simple code snippet exploring C++ limitations:
int main() {
float x = 2147483000;
int y = static_cast<int>(x);
cout << "x: " << x << ", y: " << y;
}
Why is the output showing me different values for x & y even though the float value is within int limit which is 2147483647
Code Results
x: 2.14748e+09, y: 2147483008
Why is it showing different values for x & y?
I have read your question carefully. There is a misconception, not mistake.
That is happening because the float have a certain capacity to store its precision up to 7 decimal places if the digits exceeds than the 7th digit, It would loss its precision after the 7th digit for beyond to it. Due to this reason the output is not accurate or same.
Why is it showing different values for x & y?
The default conversion for displaying float that are in the range for which an exponent is used for display is to show six significant digits.
In the format commonly used for float, IEEE-754 binary32, the two representable values nearest 2,147,483,000 are 2,147,482,880 and 2,147,483,008. So, for float x = 2147483000;, a good C++ implementation will convert 2,147,483,000 to the closest float value, 2,147,483,008.
Then int y = static_cast<int>(x); sets y to this same value, 2,147,483,008.
When the float x is inserted into the cout stream with default formatting, six significant digits are used, producing 2.14748•109.
When the int y is inserted into the stream, its full value is shown, 2,147,483,008.
You can see the full value by requesting more precision. std::cout << std::setprecision(99) << "x: " << x << ", y: " << y << '\n'; produces “x: 2147483008, y: 2147483008”.
Hello taking an online class on nested loops and this was provided as the example but I don't really know what is going on.
The following code example shows nesting for loops to output a chess or checkerboard representation using the characters X and O. Why do we need x and y variables to execute a certain amount of times. And what does alternate = !alternate; mean? About the x and y wouldn't it just do it 8 times total because its greater than the amount of times y supplies? what is the difference in purpose for the two for statements? Thank you.
for (int x = 0; x < 8; x++)
{
for (int y = 0; y < 4; y++)
{
if (alternate)
{
cout << "X ";
cout << "O ";
}
else
{
cout << "O ";
cout << "X ";
}
}
alternate = !alternate;
cout << endl;
}
The variable x used for the number of lines you want to print X-O pairs. Variable y used to specify the number of X-O pairs in 1 line. So for printing 8 lines of X-O pairs and in each line, 4 pairs of X-O, you should do just like that.
The operator '!' used for getting the opposite of a value (it's logical NOT) (for example, 1 to 0 or false to true). so alternate = !alternate; means that after every line of X-O pairs, it changes from true to false or vise versa.
So lines' first character (X or O) will change according to 'alternate' variable.
I am trying to create a program to print first 200 elements following a specific numerical series condition which is
1-1-3-6-8-8-10-20
But instead of showing, just 200 elements is showing 802. I assume is because of the code inside the for loop. I have hours thinking on how to reduce that code to the job and I cannot think anything else. I am getting frustrated and need your help.
The exercise is on the code comments
//Print the following numerical series 1-1-3-6-8-8-10-20 until 200
#include <stdafx.h>
#include <iostream>
#include <stdlib.h>
using namespace std;
int main()
{
int Num1=200, z = 0, x = 1, y = 1;
cout << "\n\n1,";
cout << " 1,";
for (int i = 1; i <= Num1; i++)
{
z = y + 2;
cout << " " << z << ","; //It will print 3
z = z * 2;
cout << " " << z << ",";//It will print 6
z = z + 2;
cout << " " << z << ",";//It will print 8
z = z;
cout << " " << z << ",";//It will print 8
y = z;
}
cout << "\n\n";
system("pause");
return 0;
}
You're looping 200 times, and each time you loop, you're printing out 4 different numbers. You're also printing twice at the start so thats 2 + 4 * 200 = 802, which is where your 802 number output is coming from.
I assume is because of the code inside the "for" loop but I've hours
thinking on how to reduce that code to the job and I cannot think
anything else. I'm getting frustrated and need your help.
So you basically wanna simplify your code. Which can be done by noticing the repetitions.
There you can find only two types of change in the series; either a +2 or x2 with the previous element.
In each iteration this can be achieved by:
If reminder i%4 == 1 or i%4 == 3, need an increment of 2 (assuming 1 <= i <= MAX)
If reminder i%4 == 0, nothing but a multiplication of 2.
When you do like so, you can simply neglect, printing of first two ones and other complications in the total numbers in the series.
Also not that, you are trying to get 200 terms of this series, which increases in each step very fast and exceed the maximum limit of int. Therefore, long long is needed to be used instead.
The updated code will look like this:
#include <iostream>
typedef long long int int64;
int main()
{
int size = 200;
int64 z = -1;
for (int i = 1; i <= size; i++)
{
if ((i % 4 == 1) || (i % 4 == 3)) z += 2;
else if (i % 4 == 0) z *= 2;
std::cout << z << "\n";
}
return 0;
}
See the Output here: https://www.ideone.com/JiWB8W
I'm confused at this example:
int x = 5;
if (x==5) cout << x; // output 5
if (x==6) cout << x;
if (x=6) cout << x; // output 6
x = 0;
if (x=0) cout << x;
x = 5;
if (x-5) cout << x;
if (x-6) cout << x; // output 5
I understand first if (x==5), but why does it output 6 at if (x=6) when x = 5, and why won't it output 0 in if(x=0)
if (x=6)
means not comparison, but assignment. You assign 6 to x and the return value of the expression is 6, which is not 0 so it gains true.
similar with if (x=0) The expression x=0 gains 0 so it means if(0)
The thing about computers is that they're extraordinarily literal. A missing semicolon, or an added character, can completely change a program's function. So you need to be just as careful as a computer when working wth programs.
As #juanchopanza alluded to, there is a difference between == and = - and you already know what it is.
I'm compiling this program using Code::Blocks 10.05 however normally I will get about 10 iterations done before it starts producing Nan in every single output. I was wondering if this is a problem caused by using the cos and sin functions and if there was a decent work around to avoid this?
I have to produce a lot of iterates because I am working on a project for University so it has to be accurate too. I looked up a few articles about how to avoid using sin and cos though I need to follow a few formulas rigorously otherwise the results I produce may be inaccurate so I'm not sure whether to compromise.
struct Particle // Need to define what qualities our particle has
{
double dPosition;
double dAngle;
};
Particle Subject;
void M1(double &x, double &y) //Defines movement if particle doesn't touch inner boundary
{
x = x + 2*y;
}
double d = 0.25; //This can and will be changed when I need to find a distance between
// the two cricles at a later stage
void M2(double &x,double &y, double d) //Defines movement of a particle if it impacts the inner boundary
{
double z = asin(-(sin(y)+d*cos(x + y))/0.35);
double y1 = y;
y = asin(-0.35*sin(z) + d*cos(x + y + 2*z));
x = y + y1 + x + 2*z;
}
int main()
{
cout << "Please tell me where you want this particle to start positions-wise? (Between 0 and 2PI" << endl;
cin >> Subject.dPosition;
cout << "Please tell me the angle that you would like it to make with the normal? (Between 0 and PI/2)" << endl;
cin >> Subject.dAngle;
cout << "How far would you like the distances of the two middle circles to be?" << endl;
double d;
cin >> d;
// These two functions are to understand where the experiment begins from.
// I may add a function to change where the circle starts however I will use radius = 0.35 throughout
cout << "So position is: " << Subject.dPosition << endl;
cout << "And angle with the normal is: " << Subject.dAngle <<endl;
int n=0;
while (n <= 100) //This is used to iterate the process and create an array of Particle data points
{ // in order to use this data to build up Poincare diagrams.
{
while (Subject.dPosition > 2*M_PI)
Subject.dPosition = Subject.dPosition - 2*M_PI;
}
{
if (0.35 >= abs(0.35*cos(Subject.dPosition + Subject.dAngle)+sin(Subject.dAngle))) //This is the condition of hitting the inner boundary
M2(Subject.dPosition, Subject.dAngle, d); //Inner boundary collision
else
M1(Subject.dPosition, Subject.dAngle); // Outer boundary collision
};
cout << "So position is: " << Subject.dPosition << endl;
cout << "And angle with the normal is: " << Subject.dAngle <<endl;
n++;
}
return 0;
}
Nan is shown in c++ as an indication of infinite, zero devision, and some other variations of non representable numbers.
Edit:
As pointed by Matteo Itallia, inf is used for infinite/zero division. I found these approaches:
template<typename T>
inline bool isnan(T value) {
return value != value;
}
// requires #include <limits>
template<typename T>
inline bool isinf(T value) {
return std::numeric_limits<T>::has_infinity &&
value == std::numeric_limits<T>::infinity();
}
Reference: http://bytes.com/topic/c/answers/588254-how-check-double-inf-nan
If the value is outside of [-1,+1] and passed to asin(), the result will be nan
If you need to check for Nan, try the following
if( value != value ){
printf("value is nan\n");
}