Does F# have a built in cube root function? I know I can use exponentiation to compute cuberoots but it won’t type check in my case since I want to take the cuberoot of a quantity of type float and get a float.
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
I don’t think there is a built-in function to calculate the cube root with units of measure (I assume it would be in the primitive operators module where
sqrtand others are), so I think the only option is to use exponentiation.However, you can use exponentiation without units and wrap the unit-unsafe operation in a function that adds units, so you get function with correct units:
Note that F# does not support fractional units so you can write
cuberoot (10.0<m^3>)orcuberoot (10.0<m^9>), but if you writecuberoot (10.0<m>)then it will not type-check, because the result would be meters to1/3(and that’s a fractional unit).This sample is only implementing
cuberootforfloat. If you wanted to write overloaded function that works with other numeric types (I guess you might needfloat32) then it gets a bit uglier (so I would not recommend that unless necessary), but you can use a trick with intermediate type with multiple overloads like, for example, in this answer.