🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

how to calculate sine and cosine in c++ without STL

Started by
23 comments, last by MrRowl 4 years, 7 months ago
Just now, Zakwayda said:

I haven't followed all the details of the thread, so maybe I'm missing some context here.

Yes. I was referring to another discussion in another topic we are having with alvaro, about optimizing sin() and acos() in a portable way but in C.

Just now, Zakwayda said:

And I wouldn't just assume OOP introduces extra overhead.

You don't have to assume. That's a well established fact. If you'd like, I can show you disassembly of C and C++ code and measurements. By default C++ wastes lot more resources (RAM and CPU) than a C code, and it requires a very experienced C++ programmer to mitigate that overhead. But let's not hijack the topic.

Just now, Zakwayda said:

It could of course, but I think you'd have to evaluate that on a case-by-case basis.

Just now, Zakwayda said:

Lastly, not using the standard library doesn't leave you with OOP-free C

Those are both perfectly correct. In the latter case I was specifically reflecting to alvaro's code which would compile as C code just as-is. For his last code, it's easy to implement both nearbyint() and copysign() with plain C macros, those are not complex functions which would require any library support. Just for the records, nearbyint is the same as adding 0.5 and casting to int, copysign is two logical operations only.

Cheers,
bzt

Advertisement
14 minutes ago, bzt said:

Yes. I was referring to another discussion in another topic we are having with alvaro, about optimizing sin() and acos() in a portable way but in C.

You don't have to assume. That's a well established fact. If you'd like, I can show you disassembly of C and C++ code and measurements. By default C++ wastes lot more resources (RAM and CPU) than a C code, and it requires a very experienced C++ programmer to mitigate that overhead. But let's not hijack the topic.

Those are both perfectly correct. In the latter case I was specifically reflecting to alvaro's code which would compile as C code just as-is. For his last code, it's easy to implement both nearbyint() and copysign() with plain C macros, those are not complex functions which would require any library support. Just for the records, nearbyint is the same as adding 0.5 and casting to int, copysign is two logical operations only.

Cheers,
bzt

Thanks for the clarification. I won't press any further because, as you note, it's (mostly) tangential to the topic at hand :)

nearbyint and copysign have been part of C for 20 years now. :)

 

One reason for avoiding the standard library implementations of things like sin/cos etc is that they're not guaranteed to give the same results independent of platform/compiler etc. If you implement them yourself using only IEEE 754 arithmetic on floats, or use a suitable library (e.g. fdlibm), and don't go overboard with your compiler's optimisation flags, you can write code that is 100% deterministic - it will produce the same output whatever computer you run it on, in debug/release, and whether you compile with MSVC, clang, gcc or whatever. 

For some applications that's really useful - especially any time you want to distribute simulations onto other computers, and be sure you can reproduce the results when they come back. Also, trying to track down the cause of tiny discrepancies in simulations can really improve your debugging skills!

 

This topic is closed to new replies.

Advertisement