News for hackers. Stuff that matters.
Re:Cosine Implementation in C by claytongulick on Friday March 31, 2023 @01:07AM (#35382833)
This is the kind of thing that triggers my imposter syndrome big time.

I've been coding professionally for almost 30 years, and I see stuff like this and wonder if I'll ever be a real programmer.

Re:Cosine Implementation in C by bigdict on Friday March 31, 2023 @01:11AM (#35382861)
Just slow down and look at it line by line. Take detours to make sure you understand every part.
Re:Cosine Implementation in C by sillysaurusx on Friday March 31, 2023 @01:28AM (#35382984)
I don’t think this would help anyone’s imposter syndrome. Taking hardcore math line by line will yield confusion.

A better approach might be to learn how cosine can be approximated with a Taylor expansion, then realize this is just an optimized version of that. (Even if it’s a different algorithm, it’s still an approximation algorithm, which is the point.)

Staring at math constants won’t get you that insight.

Re:Cosine Implementation in C by claytongulick on Friday March 31, 2023 @01:45AM (#35383087)
Sure, but that's not what causes it.

I can reason through the code and understand it.

The thing that makes me wonder if I'll ever be a real programmer when I grow up is that someone wrote this, probably on a Thursday after lunch (at least, that's how my brain imagines it).

They were able to conceptualize the math solution to a major optimization problem, deal with all sorts of precision issues with bit manipulation, and come up with a blazing fast bedrock algorithm that's dazzling both in it's simplicity and complexity. And then likely popped out to a pub for a pint and didn't think much about it again.

Intellectually, I understand that this person was specialized, and may struggle mightily navigating the complexity of modern web frameworks, something that I find straightforward because I do it every day.

But for some reason, this stuff always strikes me as being "real" programming, and what I'm doing as just plumbing, piping together the bits that the "real" programmers write.

Re:Cosine Implementation in C by nemo1618 on Friday March 31, 2023 @02:09AM (#35383267)
Do you ever feel a burning desire to just solve the hell out of a particular problem? Usually this happens in the context of performance optimization, but it can be anything. Maybe you're supposed to be working on something else, but all you can think about is your pet problem. You throw a bunch of stuff at it, and mostly it doesn't work, but the failures don't discourage you at all; making any sort of progress is addicting. You look at other people's code. You read blog posts and research papers. You fill up notepads. None of this feels remotely like work. It's fun, and you simply cannot stop until you finally grind the problem into the dust.

My guess is that the author of this code was possessed by a spirit like this. Maybe I'm wrong; maybe he was just assigned by his manager to write a cosine function that met a certain performance threshold, and he immediately had a general idea of how to do it, and banged it out in an afternoon. But I doubt it.

My point is that, if you have felt this way about programming, you are doing "real" programming. It's not about whether you're writing a web framework or a cosine function. It's about the thrill of the chase.

Re:Cosine Implementation in C by nicky0 on Friday March 31, 2023 @01:54AM (#35383137)
Your mind is inventing the part about them doing it on a thursday afternoon before popping out to the pub.

This could have taken weeks of research and refinement. You can't tell from just looking at the final result.

Give yourself some credit. If you were tasked with producing a cosine implementation, you'd pull out some textbooks, read specs, research algorithms, try a few ideas, test, iterate. You could come up with this. The only reason you didn't is that you didn't need to make a cos function yet; you had other stuff to do.

Re:Cosine Implementation in C by nicoburns on Friday March 31, 2023 @07:40AM (#35385534)
IMO the part of programming that interfaces with humans and requirements is just as tricky (in a different way) and just as important.

Also, have you ever formally studied maths? People don't just come up with this kind of thing. They read about algorithms in textbooks. They might not have read this specific algorithm (which may be original), but they'll have read lots of similar ones.

Re:Cosine Implementation in C by BeetleB on Friday March 31, 2023 @12:37PM (#35389588)
If it's any consolation, I worked with one of these teams for a few years. Brilliant at the math stuff, and many could not handle git. Or unit tests.

You've spent your time one X, they've spent their time on Y. That's it. Most of these people are math people. They may have CS degrees, but they've spent the bulk of their time in math.

Actually, now that I recall, one of them was a nuclear physicist and the other was an industrial engineer - neither had CS degrees. I don't know about the others.

Re:Cosine Implementation in C by jcranmer on Friday March 31, 2023 @01:57AM (#35383176)
You're probably more of a "real programmer" than whomever wrote this code--it's numerics code, after all.

There's no real tricks or anything to this code, it's just a polynomial evaluation of a Taylor series (which you probably learned, and subsequently forgot, from calculus), with some steps done using techniques for getting higher precision out of a floating-point value, and (in some other functions in libm, not here) occasional use of the bit representation of floating-point values. It's standard techniques, but in a field that is incredibly niche and where there is very little value in delving into if you don't have a need for it.

Re:Cosine Implementation in C by new2yc on Friday March 31, 2023 @02:34AM (#35383449)
If you don't care about "performance":

```// Uses first 10 terms of Taylor Series double my_cos(double x) { double sum = 0; for (int n = 0; n < 10; n++) { sum += pow(-1, n) * pow(x, 2 * n) / factorial(2 * n); } return sum; }```

Re:Cosine Implementation in C by irchans on Friday March 31, 2023 @08:21AM (#35385976)
/* This code is silly, not super accurate, but fun /

double cos2(double x, int n) {

    double numerator = 1.0;

    double denominator = 1.0;

    double pi = 3.14159265358979323846;

    int i;

    for (i = 1; i <= n; ++i) {
        numerator   *= pow((x - n * pi / 2.0), 2);
        denominator *= pow((n * pi / 2.0), 2);
    }

    return numerator / denominator;
}

(Edit for format)

Re:Cosine Implementation in C by Sesse__ on Friday March 31, 2023 @01:44PM (#35390503)
Also if you don't care about accuracy at all. my_cos(2 * M_PI) = 0.9965. my_cos(4 * M_PI) = -2917.7144 (!).
Re:Cosine Implementation in C by opportune on Friday March 31, 2023 @05:41AM (#35384626)
Agreed with this. Also outside of very specific applications like graphics libraries, robotics, game engines you would never want to write something like this from scratch and instead rely on an existing library or game engine. So unless you’re working in that area it’s not imposter syndrome to find it foreign, it’s probably good!

I’ve never worked directly on floating point libraries like this, but having dealt with similar stuff, I suspect if this were in production it would end up peppered with IFDEF to support various implementation differences across hardware types, even if it is compliant with eg IEEE standards.

Re:Cosine Implementation in C by Sesse__ on Friday March 31, 2023 @01:39PM (#35390405)
It's not a Taylor series. It's a series, but a minimax approximation, not Taylor.

Taylor series are essentially “Give me a polynomial p(x). At x=a, I want the value to be b, the first derivative to be c, the second derivative to be d, etc. etc.”. (Usually, a=0, also known as a MacLaurin series.) This gives you a _great_ approximation in the immediate vicinity of a, but usually a pretty horrible one the further away you get; in a sense, you spend all your approximation energy on that single point.

Minimax approximations are “Give me a polynomial p(x). Over the area x=[a,b], I want it to never stray far from f(x); give me the one that minimizes the worst-case distance”. (The distance function is usually either the absolute error |p(x)-f(x)|, or the relative error |p(x)-f(x)|/|f(x)|, but you can have an arbitrary weight function in some implementations.) So you consider everything in that area to be important, not just one point.

Taylor series are simple to calculate (which can help if you need to calculate the coefficients on the-fly, or by hand), good for analysis, and easy to understand, so most engineers will have heard of them in calculus. The Remez algorithm, on the other hand, essensially requires a computer, and is significantly more complicated to understand and implement. (Remez is not the only way to generate minimax polynomials; Chebychev polynomials come close, and I believe Sollya uses some other algorithm.) Both will converge to the actual function giving infinite terms, but minimax polynomials will be _much_ closer in most real-life situations. Also, if you are really pressed for it, you can have rational minimax polynomials (p(x)/q(x)) that approximate tricky functions _really_ well even at low degree, but of course at the cost of a division.

Re:Cosine Implementation in C by okaleniuk on Friday March 31, 2023 @02:01AM (#35383198)
This is a fascinating topic indeed. And the math behind polynomial approximation is not that deep or unapproachable. You just haven't found an explanation that works for you. Some time ago I tried to make one with a sine as an example: https://wordsandbuttons.online/sympy_makes_math_fun_again.ht...

It doesn't mention Taylor series anywhere but if you only make your system out of N differential equations, you'll get N members of the power series.

Re:Cosine Implementation in C by Mike_12345 on Friday March 31, 2023 @02:20AM (#35383357)
This is Numerical Analysis, not "programming". It's a topic in Applied Mathematics that you would need to study separately from programming. https://ocw.mit.edu/courses/18-330-introduction-to-numerical...
Re:Cosine Implementation in C by kolbe on Friday March 31, 2023 @12:31PM (#35389511)
There are 1000's of different ways to be an expert in ways that other people aren't implementing high performance functions in numerical analysis is only one.
Re:Cosine Implementation in C by charlieyu1 on Sunday April 02, 2023 @01:57PM (#35413272)
I have some math background so it is not difficult to understand.

If you ask me to write a simple API for a CRUD app though I'd cry like babies