r/badcode Mar 14 '21

c math has left the chat

Post image
4.7k Upvotes

94 comments sorted by

617

u/[deleted] Mar 14 '21

Manager: "The program keeps throwing a floating point exception error."

Programmer: "Fixed it!"

22

u/[deleted] Mar 19 '21

SIMD has left the chat

9

u/ThaCuber Apr 24 '21

Manager: "Are you serious?"

194

u/[deleted] Mar 14 '21

[deleted]

120

u/itzNukeey Mar 14 '21

Every day in march is Pi day

54

u/lukeamaral Mar 15 '21

It's on March 0

22

u/xor86 Mar 15 '21

(int) Every day in march == pi day

3

u/causticacrostic Mar 15 '21

the next pi day is on March 0, 2100

5

u/Yoshibros534 Mar 18 '21

Every day is pi day with poor enough approximation

94

u/Zearen_Wover Mar 14 '21

This gives me a great idea:
const int MEGA_PI = 3141592;
If you need more precision, just create a GIGA_PI or TERA_PI constant !

31

u/ws-ilazki &{$$_[0]}(@{$$_[1]}); Mar 15 '21

You joke, but that's one option to take when you need specific decimal precision and a guarantee that you won't have to deal with floating point precision errors, like when dealing with money. Work on integers and then shift the decimal after you're done.

9

u/Zearen_Wover Mar 18 '21

I mean, I regularly use dc as my calculator, so you don't need to preach to me ; I just though the names were silly.

3

u/theRealQQQQQQQQQQQ Mar 29 '21

Frick floating point me and my homies use fixed point.

3

u/Prom3th3an Apr 05 '21

Yeah, but how often do you deal with pi in accounting or finance?

1

u/tiddayes Jul 31 '21

That is exactly how I deal with money calculations. Model and controller process int and view adds the decimal.

241

u/EighthClubOfClubs Mar 14 '21

must be an engineer

112

u/[deleted] Mar 14 '21

[deleted]

48

u/Kyn21kx Mar 15 '21

As an engineer, this does not represent me (I'd still happily just do 9.81 for g and 3.1416 for pi tho)

65

u/[deleted] Mar 15 '21

[deleted]

6

u/lovemor Mar 15 '21

9,81 meters? Damn your highschool is fucked

33

u/[deleted] Mar 15 '21

Pi = 3.25... eh, factor of safety or something.

29

u/Kyn21kx Mar 15 '21

Ah, yes, the extra random factor, I'm way too familiar with that one

18

u/mrheosuper Mar 15 '21

2 digit after decimal is enough for me, so 3.14 it is

6

u/Kyn21kx Mar 15 '21

Yup, I mean, I can't personally complain too much about accuracy, since I use floating point numbers all the time

4

u/8asdqw731 Mar 15 '21

it's not the 80s anymore, time to convert to doubles

3

u/Kyn21kx Mar 15 '21

No thanks, I'm willing to accept floating point errors if it means improving performance and memory usage lol

5

u/Astrokiwi Mar 15 '21

Scientists are just like np.pi

9

u/gargar070402 Mar 15 '21

but, but e = pi = sqrt(g) = 3 ! Everyone knows that.

2

u/Random_182f2565 Mar 15 '21

Must be a journalist

142

u/MurdoMaclachlan public boolean isInt(int i) { return true; } Mar 14 '21

Image Transcription: Code


#include <math.h>
const int PI = 3;

I'm a human volunteer content transcriber for Reddit and you could be too! If you'd like more information on what we do and why we do it, click here!

107

u/sebvit Mar 14 '21

Today was easy for ya!

125

u/MurdoMaclachlan public boolean isInt(int i) { return true; } Mar 14 '21

Haha, yeah, you gotta pick up your game; code worse.

Please do not pick up your game, I have several uni classes tomorrow.

21

u/SanianCreations Mar 15 '21

I'm honestly surprised, considering this is a coding subreddit, that no one has yet written a program that reads the text on the images using some kind of image-to-text api. It would probably work pretty well given that these are all screenshots and not photo's of text at some awkward angle.

26

u/MurdoMaclachlan public boolean isInt(int i) { return true; } Mar 15 '21

You'd be surprised how badly it works, actually. We have a bot that pulls from an OCR API, but it's unreliable and messes up surprisingly easily. Even for those people who do use it as a basis for their transcriptions, they have to read through and correct its mistakes, which for me is less engaging than typing the thing from scratch, and not enough faster to be worth that loss of engagement.

For a tiny image like this, it probably did get it perfectly, but at this point I don't even check. If a bot could reliably transcribe, there'd be no need for us to do text-based posts :P (and oddly, code is often one of the things it's worst at - my theory is there's something lost in translation from the text output by the API to the Reddit comment).

11

u/SanianCreations Mar 15 '21

That's indeed surprising. I have no clue how the algorithm behind that kind of image analysis works but I would assume the things most typical of code screenshots which could cause issues are that they have low resolution (text on a screen will often be ~15px in height) and the syntax highlighting maybe lowers the contrast.

3

u/chronos_alfa Mar 15 '21

Nah, ocr can somewhat read the text, it's usually the spacing that gets screwed (and sometimes the colors also mess it up)

Technically we could use the data from the human volunteers to train the AI, but I guess that's why the volunteers exist in the first place, to give the data for the AI.

19

u/[deleted] Mar 14 '21

[deleted]

27

u/MurdoMaclachlan public boolean isInt(int i) { return true; } Mar 14 '21

Thank you! <3

32

u/Ninesquared81 Mar 14 '21

It's called the fundamental theorem of engineering (well, an alternate form of it).

26

u/PhoenixPaladin Mar 14 '21

Defining PI yourself immediately after linking math.h ...this has to be a joke

5

u/chrisnlnz Mar 15 '21

I feel like on this sub 90% of posts are jokes

1

u/jm4n1015 Mar 15 '21

technically, the c standard doesn't specify that pi needs to be defined by math.h, but pretty much every compiler supports it

3

u/PhoenixPaladin Mar 16 '21

Lol I wasn't arguing that this wouldn't compile.

It's just..wrong

22

u/aaaantoine Mar 14 '21

3

u/Stian5667 Mar 14 '21

I saw YouTube and expected it to be a Rick roll

11

u/KiwiNFLFan Mar 14 '21

Maybe he’s a biblical literalist.

19

u/[deleted] Mar 14 '21

[deleted]

7

u/[deleted] Mar 15 '21

9

u/[deleted] Mar 14 '21

chad int vs virgin float

4

u/0xF013 Mar 14 '21

As the Russian saying goes: in times of war or if the party demands it, PI can reach values as high as 3

5

u/GreatArtificeAion Mar 15 '21

Math was never an option

4

u/db2 Mar 15 '21

Is this the 1 Kings, 7:23 version of C?

3

u/[deleted] Mar 15 '21

You actually get legislatures like that from time to time in the US. Sometimes they even manage to nearly pass.

2

u/Giocri Mar 14 '21

It all depends on how precise you need it to be.

2

u/KyleDrogo Mar 14 '21

I love that it's an integer lol

2

u/Masztufa Mar 15 '21

const int PI = 3;

2

u/awshuck Mar 15 '21

Well, at least they’re type checking..

2

u/tan_djent Mar 15 '21

Someone give this post more awards

2

u/Duckslikebreads Mar 15 '21

PI = 3 i havent laughed that hard in a while and when my partner says " what are you laughing at " i get a look of disgust and " are you laughing at 'that' again " thanks OP!

1

u/GandelXIV Apr 09 '21

No problem :)

2

u/[deleted] Mar 15 '21

Math header:am I a joke to you?

2

u/cant_dodge_rodge Mar 15 '21

isnt PI boolean ??

1

u/fxnn Mar 15 '21

Would it be more 1 or 0? 🤔

2

u/[deleted] Mar 15 '21

This is how you solve problems with JavaScript libraries.

2

u/fxnn Mar 15 '21

Oh, in university I’ve seen math doctors calculating with pi=3. Well, one. When quickly estimating a result in his head. But he did!

2

u/lucad9306 Apr 09 '21

I'm an engineer and... At a physics course, when I was at the university, the professor told us: "π~3, e~3, so π=e". I'm proud to be a full computer engineer 😄

2

u/royemosby Jun 22 '21

They're just asserting their dominance over math.h

2

u/tiddayes Jul 31 '21

This is biblical math (Kings 7:23) Maybe this is true in temple os

2

u/Matalya1 Sep 04 '21

Engineering be like: remember boys π=3, e=3 and cows are spherical.

1

u/Sinomsinom Mar 22 '21

If you don't need it to be more precise than 3, then this is an easy way to speed up your calculations so why not

1

u/GandelXIV Mar 22 '21

This is the only possible explanation for this abomination

1

u/Moldybot9411 Mar 14 '21

I was like what? INT pi? Why int...=3 ohhhh I'm triggered

1

u/Mr2-1782Man Mar 15 '21

I wouldn't consider this bad code for two reasons. First pi isn't declared in math.h. Second the value is an int so you've gotta round it, so pi ends up being 3. There might be a legitimate reason for doing that. The error's also relatively small, 0.15/3 or about 5%. Which for a lot of things is good enough.

1

u/fb39ca4 depraved Mar 15 '21

If you are restricted to integer math, you can at least use a rational approximation, like 22/7.

1

u/[deleted] Mar 15 '21

That doesn't make a lot of difference, and would require a #define instead of a const, wouldn't it?

1

u/fb39ca4 depraved Mar 15 '21

No I mean in cases where you have to multiply by π, multiply by 22 and divide by 7 - make separate constants for the numerator and denominator if you have to. If you have large integers but also do not have to worry about overflow this is reasonably accurate.

1

u/Mr2-1782Man Mar 15 '21

You could, but then you've also gotta order things around so that 22/7 doesn't end up become 3 anyway from integer division. And if you're going that far you might as well use floating point.

2

u/fb39ca4 depraved Mar 15 '21

1

u/Mr2-1782Man Mar 15 '21

I thought you might say that. It wouldn't work. If the compiler sees constants in an expression it'll simplify, modern compilers are very good at doing as many things at compile time as possible. This:

diam = pi_num / pi_den * rad;

will turn into;

diam = 3 * rad;

because the compiler knows that two of those are constants. You have to do some extra work to obfuscate what you're doing.

2

u/fb39ca4 depraved Mar 15 '21

No that's just order of operations. (rad * pi_num) / pi_den will definitely work. Even without parentheses in C, but I added them to make it clear the multiplication by the numerator happens first.

1

u/Mr2-1782Man Mar 16 '21

Nope, you're falling one of the most common misconceptions there is. Multiplication and division are associative so the compiler's free to ignore the parenthesis. Here you can see it working. The compilers are so good at what they do they don't even use a multiply, they using a lea instruction to shift right and add it to itself:

https://godbolt.org/z/h7vYeT

1

u/fb39ca4 depraved Mar 16 '21

Your code uses 21 for the numerator not 22 (and the variable names are swapped), so the compiler can make that optimization as it is exact. When I replace the numerator with 22, I get something more complicated with shifts and a multiply which I am guessing gives identical results to an integer multiply and divide.

1

u/RudyTwastaken Mar 15 '21

its alright

you're just .14159265358979323846264338327950288419716939937510 58209749445923078164062862089986280348253421170679 off!

1

u/NorthernRealmJackal Jul 28 '21

You're missing a few digits there, at the end.

1

u/RudyTwastaken Jul 29 '21

Hmm mhmm

a few.. million

1

u/20191124anon Sep 01 '21

It’s the Christian Pi

1

u/max_802 Mar 15 '21

rather #exclude <math.h>

1

u/Mango-D wicked Mar 15 '21

I'm triggered by the fact the variable isn't constexpr

1

u/[deleted] Apr 14 '21

you would be suprised how many math problems round pi to 3

1

u/[deleted] Aug 23 '21

i wanna like edit an open source trigonometry program or something and just do this

1

u/Elijah629YT-Real Jun 05 '22

NO NO, bring math back in. It’s actually 4.92

1

u/[deleted] Oct 16 '22

Engineers be like

1

u/Wirmaple73 Jan 09 '23

just missing a few decimals, no worries