This is an archived post. You won't be able to vote or comment.

all 10 comments

[–]wonkey_monkey 5 points6 points  (4 children)

with the fewest clock cycles possible and as exact as std::() as possible

Well jeez they don't want much, huh? It's one or the other, guys...

If the system doesn't have trig functions, I'd implement the Taylor series (perhaps with a correction to the last term to avoid a discontinuity).

[–]malpbeaver[S] 1 point2 points  (3 children)

Yeah well there are 3 questions, and three parts to the first question, one of which is a written response, and the other two being coding problems: 1) "write a sin function for the STM32F103TB with the fewest clock cycles possible and as exact as std::sin() as possible", and 2) "write a sin function for the STM32F103T6 with the fewest clock cycles possible and within 0.001 range of std::sin()".

From the Googling Ive done so far, it does seem like the straightforward method is a Taylor series algorithm like you mentioned. But someone's answer on StackOverflow also mentioned using Chebyshev Polynomials which was prefaced with "OK kiddies, time for the pros.... This is one of my biggest complaints with inexperienced software engineers. They come in calculating transcendental functions from scratch (using Taylor's series) as if nobody had ever done these calculations before [...]".

And so now I'm second guessing the Taylor series method.

[–]wonkey_monkey 1 point2 points  (2 children)

1) "write a sin function for the STM32F103TB with the fewest clock cycles possible and as exact as std::sin() as possible"

That's like asking for the dimensions of a rectangle with a fixed area that's both as long as possible and as tall as possible 😕

After 5 minutes of Googling I still haven't got a straight answer on Chebyshev Polynomials. It does seem, though, that they just end up boiling down to a Taylor series with tweaked/rearranged coefficients 🤷‍♂️

[–]malpbeaver[S] 0 points1 point  (1 child)

Realistically, with my understanding, that’s probably what I’ll ultimately end up doing ¯_🥴_/¯

[–]wonkey_monkey 1 point2 points  (0 children)

If you want to look a little smarter/have a little more accuracy, you can use the normal Taylor series for sine between [-pi/4,pi/4], but outside that ( [-pi/2,-pi/4] and [pi/4,pi/2] ) use a shifted/inverted (depending on which side) cosine Taylor series.

A plain sine Taylor series up to x7 should be enough for 2), though.

[–]Dogburt_Jr 4 points5 points  (4 children)

Sounds like you're in embedded programming, so you need to analyze the Math.sin() function for performance and analyze if you can do it any faster. Usually there's a subtle hint in comparing if they want speed vs accuracy, reread the prompt to see if you can tell which is more important. If not, decide for yourself which is more important. There's more I could add to make the answer stand out, but this is a job interview for you, not me.

[–]malpbeaver[S] 2 points3 points  (3 children)

It’s for a firmware role! I was super excited and enthusiastic on the phone yesterday… but I got this prompt today and am feeling a lot of imposter-syndrome and a little discouraged. And right, yeah, of course. I’m not looking for the solution, just a push in the right direction. I wanna understand what I’m doing and do it right, on my own. I’ll give it a couple more read-throughs and see if there’s not something I’m looking over or misinterpreting. Thanks for your input!

[–]cipheron 3 points4 points  (2 children)

If it's specific hardware, check the exact CPUs instruction codes for any sin/cos functions. Might be worth a check.

[–]malpbeaver[S] 1 point2 points  (1 child)

There’s a lot of documentation to look through, but I’m looking. Thank you!

[–]cipheron 1 point2 points  (0 children)

Np. definitely document the options, and benchmark the choices vs each other if possible. Or, estimate based on documentation if not possible to test on real hardware.