This question already has answers here:
What does this C statement mean?
(4 answers)
Closed 10 years ago.
What does those notations refer to ? What am i declaring ? I have a hard problem identifying what is being declared
1 - double (*b)[n];
2 - double (*c[n])();
3 - double (*d())[n];
And i can't even think in understanding this one
double (*foo(double (*) (double, double[]),double)) (double, ...);
Overall, if there is any logic or step-by-step i can use for almost all ( or preferably al )
cases, would be really great to know.
double (*b)[n];
This is a pointer named b that points to an array of doubles that has length n.
double (*c[n])();
This is an array named c of n pointers to functions that take in unspecified arguments (in C) or no arguments (in C++) and return doubles.
double (*d())[n];
This is a function named d that returns a pointer to an array of n doubles.
Hope this helps!
Related
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
This question already has answers here:
John Carmack's Unusual Fast Inverse Square Root (Quake III)
(6 answers)
Closed 6 years ago.
I am looking an example code for invert sqrt used in quake.
I see a variable of type float: float x = 16;
and then an int variable takes the value of this expression: int i = *(int*)&a;
The way I understand it(from right to left) is that it takes the address of the variable a, typecasts it in to an integer pointer and then takes the value of that pointer and assigns it to i.
when I output the variable i in the console it's a big integer value.
can someone explain more in dept of what is going on here please ?
because I expected the variable i to be 16.
&a takes the address of a, a float; (int *) keeps the same address but tells the compiler now to pretend that an int is stored there; int i = * then loads the int from that address.
So you get* an integer with the same bit pattern as the float had. Floats and ints have very different encodings so you end up with a very different value.
The float is likely to be formulated like this but it's not required; your architecture may do something else. Possibly the author is doing a platform-specific optimised version of frexp.
int i = (int)x; would simply convert the float to an int, giving you 16.
This question already has answers here:
Handle arbitrary length integers in C++
(3 answers)
Closed 7 years ago.
Hi I am doing an algorithm question require get the full result of
5208334^2, which is 27126743055556
I was able to do it with by represent integer using Charracter array. However can we have any better way (shorter or faster) to do that? any idea is welcome ?
Updated:
For my case, both long long and int64 work, just that I did not cast value before return:
int val (int n1, n2) {
........
return (long long) n1 * n2;
}
This number fits into long long(present in GCC and after c++11) type or int64(for some other compilers before c++11). Thus the simplest solution is to use this type.
This question already has answers here:
What is the most effective way for float and double comparison?
(34 answers)
Closed 9 years ago.
I'm writing a function which accepts a double data type, say variable 't', and if 't' is equal to 0.05, the function does something.
Now, my problem is that if 't' is 100-99.5, the function fails to work. But it works for 't' = 0.05-0. I'm guessing this is something to do with how numbers are stored. What's the work around?
PS : I'm not a programmer, merely using C++ as a tool, so I don't know much about it other than the basics. A simple solution would be very appreciated.
In c++ or any language == operator is not recommended for double or float type because the variable stores approx value not the exact value with very slight error. Lets say you are using double variable t to compare equality with 0.05 you can use the following code:
if( t-0.00000000001<=0.05 && t+0.00000000001>=0.05 )
It will work fine in this case
This question already has an answer here:
Logarithm function of an arbitrary integer base in C
(1 answer)
Closed 6 years ago.
I've been playing around with some math recently and I would like to know if anyone has written/seen a C++ implementation of log that one can specify the base (root..?) for? As in:
Mathematical function definition http://i1091.photobucket.com/albums/i383/dannydeth1/forumla.png
Obviously I would prefer giving the base as an argument: double d = log(b,x);
Thank you for your time and any answers are much appreciated. :}
EDIT: Also, I take it would use Taylor Series?
log_b_(x) = log(x) / log(b). Just do this:
double log(double base, double x)
{
return std::log(x) / std::log(base);
}
It's straightforward to implement yourself:
double
logb( double n, double b )
{
return log(n) / log(b);
}
Is it generally useful? Or are practically all of the uses subsumed by log, log10 and log2?