Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

You're absolutely 100% correct but to play the devil's advocate, what if he is talking about it being

glibc:

> return ( j >= 0 ) ? j : -j;

Musl:

> return a>0 ? a : -a;



It really doesn't matter, the compiler will generate the same code (gcc -S at least does generate the same assembler output for both pieces of code; see: https://gist.github.com/reidrac/afb89b29228a243954bd51281c39...).

I guess looking at the definition: https://en.wikipedia.org/wiki/Absolute_value#Real_numbers I would say `( j >= 0 ) ? j : -j` is more accurate, although in practical terms is the same result.


They'd differ for floating-point NaNs.


But the input types here are int's.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: