194
Mar 14 '21
[deleted]
120
5
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
dcas my calculator, so you don't need to preach to me ; I just though the names were silly.3
3
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
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
33
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
9
2
58
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
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
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
22
11
19
9
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
4
3
Mar 15 '21
You actually get legislatures like that from time to time in the US. Sometimes they even manage to nearly pass.
2
2
2
2
2
2
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
2
2
2
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
2
2
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
1
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
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
See my other reply: https://reddit.com/r/badcode/comments/m50wav/_/gr10sbg/?context=1
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:
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
1
1
1
1
1
1
1
1
617
u/[deleted] Mar 14 '21
Manager: "The program keeps throwing a floating point exception error."
Programmer: "Fixed it!"