This question already has answers here:
C++ cast syntax styles
(10 answers)
What is the difference between C-like casting and functional casting? [duplicate]
(4 answers)
Closed 3 years ago.
Suppose I have a floating point number x and I want to convert it into an integer. Is there any difference between int(x) and (int)x in C++.
I am new to C++ coming from Python background and naturally I tried int(x) to convert x to an integer but reading about C++ I came across type-casting i.e. (int)x and in this case it also works.
I was wondering what's the difference between these two approaches and in a wider context which one to use where?
float x = 5.5;
cout<<int(x)<<endl; //outputs 5
cout<<(int)x<<endl; //outputs 5
Related
This question already has answers here:
Dividing two integers to produce a float result [duplicate]
(1 answer)
What is the behavior of integer division?
(6 answers)
Closed 9 months ago.
I'm new at programming stuff and i'm practicing with variables, so i made a code in C++ where basically i do something like this:
float a;
a=4/3;
printf("%f", a);
return 0;
i expected to get the result as 1.333, but instead i got 1 as value
But if i do this:
float a, b=4, c=3;
a=b/c;
printf("%f", a);
return 0;
It gives back the right value (1.3333)
can somenone please explain that to me?
Ah, C++ being less than helpful. This is going to annoy you.
4 / 3
This is INTEGER math, not floating point. This would work if you did one of these:
4.0 / 3
4 / 3.0
4.0 / 3.0
C++ won't upscale your values to floating point while doing the calculation unless there are floating point values somewhere in the calculation itself.
This question already has answers here:
C/C++: Pointer Arithmetic
(7 answers)
Closed 6 years ago.
Consider the code:
int arr[20]{};
int * ptr1=arr, * ptr2=&arr[1];
std::cout<<ptr1<<std::endl<<ptr2<<std::endl<<ptr2-ptr1;
Output:
0x7fff4003e0d0
0x7fff4003e0d4
1
Why it isn't 4 instead?
If it didn't to that you'd just end up dividing the difference by sizeof(whatever) all the time. The number of elements is far more useful than the raw difference. When you need the latter, cast the two pointers to char* for the subtraction.
This question already has answers here:
Representing big numbers in source code for readability?
(5 answers)
Closed 7 years ago.
In C++, sometimes you want to declare large numbers. Sometimes it's hard to see if you have the right number of zeroes.
const long long VERY_LARGE_NUMBER = 300000000000;
In a language like OCaml, you can separate numbers with underscores to improve readability.
let x = 300_000_000_000;;
Is there a similar mechanism in C++? I have seen things like = 1 << 31 for powers of 2, but what about for very large powers of 10? Sometimes you're declaring very large numbers (e.g. array bounds in competition programming) and you want to be confident that your declared array size is correct.
I can think of something like:
const long long VERY_LARGE_NUMBER = 3 * (1 << (11 * 10 / 3));
...which abuses 1<<10 ~= 1000 get close to 3 with 11 zeroes, but it's verbose and not exact.
how about
const long long VERY_LARGE_NUMBER = (long long) 300 * 1000 * 1000 * 1000;
Since C++14, integer literal supports the use of ' as a delimiter. For example, unsigned long long l2 = 18'446'744'073'709'550'592llu;. See this cppreference page for the details. Also, you may consider using scientific notation, like 123e4. Such literals are floating point literals. But you can convert them to integer types.
This question already has answers here:
Dividing 1/n always returns 0.0 [duplicate]
(3 answers)
Closed 8 years ago.
when i try to calculate any expression, I always get an integer result, it's like:
float k= 5/12;
std::cout<< k<<std::endl;
the output in the console is always 0.
In C/C++, this is an integer division:
5/12
What you want is a floating point division:
5.0/12.0
Please note that this has absolutely nothing to do with GLUT or OpenGL.
This question already has answers here:
Why pow(10,5) = 9,999 in C++
(8 answers)
Closed 8 years ago.
Using some version of minGW, the following code will print 99.
int high;
high = pow(10,2);
std::cout<<high<<std::endl;
The parameter of pow function is double, but why i get 99? Someone who can tell me the process hidden inside pow function ?
Converting a double to an integer truncates the fractional part. pow(10,2) produces a slightly inaccurate result; if it's slightly high, you'll get 100 and if it is slightly low you'll get 99.
Moral: if you mean i*i, write i*i.