I don't like line 5 above. dx should be an infinitely small number.
The value chosen above may work for a sin function, but if I'm calculating molar mass, it's way too big. Even worse, if I'm doing orbital mechanics, it's so small that it will be less than the LSB of the mantissa of a floating point number which will have the disastrous effects of not incremental x at all.
Is there a function available in std:: that will return the value of the LSB of the mantissa? This would let me calculate dx properly for any value of x.
I've been checking in <limits> but I haven't found anything and I don't want to dig into IEEE754 to do it bitwise myself.
Edit: I've continued researching and have found that what I am looking for is the ULP (Unit in last place) of the floating point number.
I know that 1 should make this prone to errors, but I just evaluated the cos(x) = 0 (which calculates to pi/2) and got it accurate to 17 places (which was the most I could display in the console). I'm happy with that.
Just a comment, the example on that cppreference page shows exactly how to scale the epsilon to get the desired ulps. And the seealso points at the <cmath> functions to get the next/previous representible float/double given any starting point.