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.
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.
Originally Posted by brewbuck:
Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.
Ah, yes, the C++ board. It was originally C code.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.Originally Posted by Mario F.
http://groups.google.com/group/comp....da8537a?hl=en&
7. It is easier to write an incorrect program than understand a correct one.
40. There are two ways to write error-free programs; only the third one works.*
Ah! Excellent. I was wondering what possible applications it could have.
Originally Posted by brewbuck:
Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.
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.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.
All the buzzt!
CornedBee
"There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
- Flon's Law
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]; };
Last edited by Mario F.; 11-10-2006 at 06:47 AM.
Originally Posted by brewbuck:
Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.
"Friendlier"?
Unless int on the particular platform has very weird alignment requirements, they're pretty much exactly he same.
All the buzzt!
CornedBee
"There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
- Flon's Law
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.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.
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.
Originally Posted by brewbuck:
Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.
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.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[].Along that line,
Last edited by jafet; 11-11-2006 at 05:01 AM.
Code:#include <stdio.h> void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){ puts(a);return;}for(;c<='9';++c){for(f=0;f<9;++f)if(a[i-i%27+i%9 /3*3+f/3*9+f%3]==c||a[i%9+f*9]==c||a[i-i%9+f]==c)goto e;a[i]=c;J(a);a[i] ='0';e:;}}int main(int c,char**v){int t=0;if(c>1){for(;v[1][ t];++t);if(t==81){J(v[1]);return 0;}}puts("sudoku [0-9]{81}");return 1;}
Ah, Pythagorean triples revisited. I thought it looked strangely familiar.Originally Posted by jafet
7. It is easier to write an incorrect program than understand a correct one.
40. There are two ways to write error-free programs; only the third one works.*