(Almost) all numbers in my program are parsed and treated as complex numbers. There is one corner cases, that specifically needs an integer. Unfortunately my programming languages does not allow to compare integers to complex numbers so I needed to come up with the following code:
function_name(Complex c)
if c has imaginary part != 0
throw exception
if ( integer(real(c)) - real(c) ) != 0
throw exception
return integer(real(c))
I'm stuck on how to call this function. If I call it something like complex_to_int
I think it hides the fact, that it fails on non integer complex numbers. If the complex number is real, then I (or other people) might expect this function to work like a normal cast from a floating point value to an integer (i.e. the decimal places are just cut off).
My question is:
-Is there a good name to call this function
Or, at the risk of the question being too subjective
-Should I split this function in two and actually have a real casting function (that just ignores potential imaginary parts and cuts of decimal places) and write a function that checks if the complex number fulfills my criteria before casting it?