Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

Generic algorithms

Name: Anonymous 2008-11-28 11:31

Implement a single function that takes two arguments and returns the bigger of the two. Assume you don't know the type of the  arguments and you don't know if the types can be compared. Assume that if a type can be compared, it will always be implemented by following a single standard. Use the latest standard of your language. Write the simplest program
that will pass an integer 1(one) and a float 1.1(one point one) into the function and write the result to standard output.

Post the compiler you used, the code and the result.
Write what will happen if the max function is called with broken syntax (I'm looking at you C macros).
Write what will happen if the objects of a type can't be compared.

I'll start with C and C++.
Compiler: gcc 4.3.2 20081105 (Red Hat 4.3.2-7)
#include <iostream>
template<class F>
F& max(F& a, F& b) { return (a < b) ? b : a; }
int main() { std::cout << max(1, 1.1) << '\n'; }

Results: Compile time error:
max.cpp: In function ‘int main()’:
max.cpp:6: error: no matching function for call to ‘max(int, double)’

Bad syntax: Standard compile-time error
No comparison: Standard compile-time error about undefined operator<

#define max(a, b) (less(a, b) ? b : a)
#include <stdio.h>
int less(int a, int b) { return a < b; }
int main() { printf("%f\n", max(1, 1.1)); }

Result: Bad output
1.000000
Bad syntax: Depends on the error in the syntax. Can either compile and cause undefined behaviour or fail at compile time with strange syntax errors.
No comparison: Standard compile-time error about an undefined function.

Name: Anonymous 2008-11-30 20:19

>>39

No, you are a fucking idiot. To expand and spoon feed you more on the example I already provided, lets use that function to see which is later, January 2nd 2009 or February 1st 2009.

If I wanted to compare some user inputted string it would be called like this:
Console.WriteLine(Compare<DateTime>.Max("01/02/2009", "02/01/2009"));
And the output is correct: 02/01/2009

A britfag would use the same inputs, and get the same result as is. To a britfag, the result reads that January 2nd is later than February first. This of course is fucking wrong, and you somehow think that makes it more compatible.

Using System.Threading.Thread.CurrentThread.CurrentCulture, will correct the comparison (assuming the thread is set with something like: System.Threading.Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("en-GB");

Not only will the program behave as expected for peoples all over the world, it is a simple change that will not blow anything fucking up. There is no chance that would introduce a bug. It fixes the bug of the function not accounting for the format of different data types in different cultures.

The fact I initially gave a very example, you didn't understand it, then claimed it would 'blow up' despite contending that the globalization feature makes apps more compatible, ad the best way to use it is to not use it (format provider is required in the call to ToType, and specifying InvariantCulture means don't apply any formatting). As in the simple date example, formatting is important.

In summary, you are an idiot faggot.

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List