This one got me curious.
I believe you are missing a couple of casts there. But regardless, I think it's an handy function that checks for =, <, and > between the arguments with just one return statement.
Printable View
This one got me curious.
I believe you are missing a couple of casts there. But regardless, I think it's an handy function that checks for =, <, and > between the arguments with just one return statement.
Ah, yes, the C++ board. It was originally C code.Quote:
Originally Posted by Mario F.
One that avoids the "obvious" implementation of a compare function commonly used with qsort, but is correct in its handling of integer overflow.Quote:
Originally Posted by Mario F.
http://groups.google.com/group/comp....da8537a?hl=en&
Ah! Excellent. I was wondering what possible applications it could have.
I'll add a really simple one, but can be handy sometimes if you wanna avoid modulo operator
what would you print at the question marks?
Code:if (x & 1)
{
cout << "x==???" << endl;
}
else
{
cout << "x==???" << endl;
}
It's less portable than the modulo operation.Code:if (x & 1)
{
cout << "x== odd maybe" << endl;
}
else
{
cout << "x== even maybe" << endl;
}
Ah, I was wondering about the void pointers.Quote:
Originally Posted by Dave_Sinkula
I used to do the &1 trick until I got smart and realized that
1) it doesn't work with 1's complement signed integers and
2) the compiler optimizes %2 to it anyway where it works.
you cannot say in general that compilers optimize that. Theres lots of compilers for embedded platforms where no attention is put in optimizing things at all.
it's non portable though indeed
Speaking of which,
(From a book someone lent me a few days ago)
Which is friendlier and why?
Code:Class X {
public:
X(): a(1), c(2) {}
private:
int a;
char b[4096];
int c;
};
Class Y {
public:
Y(): a(1), c(2) {}
private:
int a;
int c;
char b[4096];
};
"Friendlier"?
Unless int on the particular platform has very weird alignment requirements, they're pretty much exactly he same.
Yeah. What would the difference be?
The general rule to ensure that the least amount of space is wasted due to alignment is to put all the things with the most restrictive alignment requirement at the beginning (eg doubles) and those with the least restrictive requirement (chars) at the end.
Yep. But also, with a standard compiler, objects will be layed out in declaration order. X may incur in a data cache miss, due to a and c almost certainly being placed on different cache lines, separated by 4096 bytes.
Okey, here comes a tough one, feel free to comment my code if it could have been written better:
Code:#include <stdlib.h>
#include <stdio.h>
#include <cfloat>
#include <complex>
using namespace std;
int main() {
double x=0, y = 1, i=0;
do {
x += y;
i ++;
y /= i;
} while (y >= x*DBL_MIN);
std::complex<double> z(-x, 0);
double a;
do {z = sqrt(z);} while (z.imag() > 0.00004);
x = z.real()-1;
y = z.imag();
a = y/x;
i = 1 - a*a;
a *= 2;
y -= x*x/2 * a;
x -= x*x/2 * i;
a = y/x;
printf("%1.16lf\n", a);
//Bonus part
#define myint int
myint n1, n2, n, t1, t2, t;
t1 = n2 = 1; t2 = n1 = 0;
for (i = 0; i < 4; i++) {
x = floor(a);
t = myint(x)*t1+t2;
n = myint(x)*n1+n2;
t2 = t1; t1 = t;
n2 = n1; n1 = n;
a = 1/(a-x);
}
printf("%d/%d = %1.9lf\n", t, n, (double)t/(double)n);
system ("pause");
return 0;
}
Mmm...kay.Quote:
as oppposed to obfuscating code.
What does M contain? Spoiler:Code:int x = 25; //any value will do
std::map <std::pair <int, int>, int> M;
for(int m = 1; m < x; ++m)
for(int n = m+1; n < x; ++n)
if(n*n + m*m < x*x)
if(n*n - m*m < 2*m*n)
M[std::pair <int, int> (n*n - m*m, 2*m*n)] = n*n + m*m;
else
M[std::pair <int, int> (2*m*n, n*n - m*m)] = n*n + m*m;
Code:for(std::map<std::pair <int, int>, int>::const_iterator i = M.begin(); i != M.end(); ++i)
std::cout << i -> first.first << " " << i -> first.second << " " << i -> second << "\n";
foo() can be passed to std::qsort() for sorting int[].Quote:
Along that line,
Ah, Pythagorean triples revisited. I thought it looked strangely familiar.Quote:
Originally Posted by jafet