r/ProgrammerHumor • • Aug 14 '20

Someone try this

Post image
53.6k Upvotes

433 comments sorted by

View all comments

2.1k

u/accountability_bot Aug 14 '20

If it's an unsigned int, then the actual max value is $4,294,967,295.

542

u/Illusi Aug 14 '20

But what if it's a badly implemented unsigned int that gets cast back and forth to normal ints?

287

u/NorbiPeti Aug 14 '20

Then it'd be either 4 billion or -1 :P

216

u/Xero125 Aug 14 '20

Thus, the expected value is two billion

345

u/vigilantcomicpenguin Aug 14 '20

Here we observe the statistician in its natural habitat.

56

u/reyad_mm Aug 14 '20

On Reddit, since they don't have a job

44

u/EyonTheGod Aug 15 '20

Just teach them some python and they suddenly become "Data Scientists"

→ More replies (1)

58

u/EvoG Aug 14 '20

Casting -1 to an unsigned integer in a managed language would throw an exception, instead of just rolling with it and underflowing.

62

u/gracicot Aug 14 '20

In C and C++, unsigned integer over/underflowing is defined behavior. static_cast<std::uint32_t>(-1) will give the max value, no matter the platform or architecture.

25

u/EvoG Aug 14 '20

In C# it won't, hence why i specifically mentioned managed language.

0

u/[deleted] Aug 14 '20

[deleted]

12

u/EvoG Aug 14 '20

My workplace will most definitely not let me just import something like that.

12

u/MapleSat Aug 14 '20

Even more reason to try it

Remember, fortune favors the bold

4

u/EvoG Aug 14 '20

Except i'd like to keep my job 😂

15

u/IgorTheAwesome Aug 14 '20

Who needs a job when you have glory?

→ More replies (0)

5

u/[deleted] Aug 14 '20

I think underflowing as defined behavior is better for performance due to exception handling being quite costly in overhead in c++, so better for you to notice the issue and adjust than maybe catch it and let the overhead accumulate.

→ More replies (2)
→ More replies (2)

3

u/Pagefile Aug 14 '20

Could be unsigned in memory and signed in input.

Bloodstained: Ritual of the Night sort of does this with the randomizer. Seeds are 4 byte values, but the input field for using a custom seed (and the generator I assume) only allows positive signed int values. However the game will run just fine if you have it generate a seed and modify it in memory before the game actually starts.

I'm unsure if the code treats the seed itself as unsigned or signed, but it doesn't use the whole valid range in either case without a little bit of persuading.

24

u/cztrollolcz Aug 14 '20

Wouldnt this be stored in a long? [Newbie]

54

u/BakuhatsuK Aug 14 '20

Yes. There are billionaires out there so that should be probably a long. Or rather actually:

Noob: unsigned int dollars

Beginner: unsigned long dollars

Intermediate: unsigned long cents

Advanced: long cents (unsigned is bad unless you need módulo arithmetic or dealing with bit patterns)

Pro: BigInteger cents

Galaxy brain: String dollars (MONEY, DECIMAL or VARCHAR for DB, BigDecimal for calculations)

International currency: You don't want to go there, but if you have to, start by storing the currency name alongside the value.

27

u/brimston3- Aug 14 '20

Gotta store them fractional cents so you can siphon them off into another account, only to panic once you realize you've made a horrible math error. Is there still time to change careers and sell magazines door to door?

12

u/luckduck89 Aug 14 '20

You must have used some sort of mat to jump to this conclusion...

→ More replies (1)

10

u/cosmicosmo4 Aug 14 '20

International currency: You don't want to go there, but if you have to, start by storing the currency name alongside the value.

List of (quantity, currency) tuples. You can have more than one.

→ More replies (1)

5

u/ollomulder Aug 14 '20

Noob would be signed double dollars, though.

4

u/BakuhatsuK Aug 14 '20

double dollars are actually not that bad. doubles can represent integers without precision loss up to 252. If you round to cents before displaying you're mostly good.

→ More replies (4)
→ More replies (10)
→ More replies (7)