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!
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.
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.
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).
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.
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.
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 😄
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.
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.
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.
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.
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:
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.
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.
613
u/[deleted] Mar 14 '21
Manager: "The program keeps throwing a floating point exception error."
Programmer: "Fixed it!"
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!
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_PIorTERA_PIconstant !244
u/EighthClubOfClubs Mar 14 '21
must be an engineer
58
u/nyaisagod Mar 14 '21
int
141
u/MurdoMaclachlan public boolean isInt(int i) { return true; } Mar 14 '21
Image Transcription: Code
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!
32
u/Ninesquared81 Mar 14 '21
It's called the fundamental theorem of engineering (well, an alternate form of it).
25
u/PhoenixPaladin Mar 14 '21
Defining PI yourself immediately after linking math.h ...this has to be a joke
23
u/aaaantoine Mar 14 '21
gasp
10
u/5p4n911 Mar 14 '21
r/foundtheengineer
10
u/KiwiNFLFan Mar 14 '21
Maybe he’s a biblical literalist.
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
4
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?
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
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/tribak Mar 15 '21
PIish
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/[deleted] Mar 15 '21
Math header:am I a joke to you?
2
u/cant_dodge_rodge Mar 15 '21
isnt PI boolean ??
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/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/Igotbored112 Mar 15 '21
Relevant xkcd
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
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/RudyTwastaken Mar 15 '21
its alright
you're just .14159265358979323846264338327950288419716939937510 58209749445923078164062862089986280348253421170679 off!
1
u/20191124anon Sep 01 '21
It’s the Christian Pi
0
u/MrPotatoAim313 Mar 14 '21
Yeah that seems about right
0
u/[deleted] Mar 14 '21
[deleted] — view deleted comment
0
u/[deleted] Mar 14 '21
[deleted] — view deleted comment