in c-programming, if I use the variables k, X, Y, n in the equation below (where exp() is math constant e raised to (), pi = 3.14159…, and j = sqrt(-1)) and these variables are all declared as 64b double precision floating-pt numbers (where X is a complex number), the result for output will also be this data type.
output = k * exp(2*pi*j*X*Y/n)
Now, if I want to try to manage memory more intelligently, I’d like to make
k, Y, and n to be 32bit unsigned long int (0 to 4294967295), and
pi, j, and X to be 32bit float.
My question is, will the result for output be data type float (hopefully) or unsigned long int? That is, does C language have some default rules that intelligently takes care of multiplying a float and integer and returning the result as float, or must I manage that manually using type casting, etc? Just wondering if there’s anything I need to worry about in such an operation, or if I can leave it to C to do everything intelligently behind the scenes.
This can get kind of ugly. The compiler looks at the types of the operands for a single operation, and promotes both to the “larger” type (e.g., if one is
intand the otherdouble, it’ll convert theinttodouble, then do the operation).In your case, that could have some rather unexpected results. Right now you have:
2*pi*j*X*Y/n. The operators group from left to right, so this is equivalent to((((2*pi)*j)*X)*Y)/n. In this case, that’ll probably work out reasonably well — one of the operands in the “first” operation is a float, so all the other operands will be converted to float as you want. If, however, you rearrange the operands (even in a way that seems equivalent in normal math) the result could be completely different. Just for example, if you rearranged it to2*Y/n*pi*j*X, the2*Y/npart would be done using integer arithmetic because2,Y, andnare all integers. This means the division would be done on integers, giving an integer result, and only after that integer result was obtained would that integer be converted to a float for multiplication bypi.Bottom line: unless you’re dealing with something like a large array so converting to smaller types is likely to really save quite a bit of memory, you’re generally much better off keeping all the operands of the same type if possible. I’d also note that in this case, your attempt at “managing memory intelligently” probably won’t do any good anyway — on a typical current machine, a
long intand afloatare both 32 bits, so they both use the same amount of memory in any case. Also note thatexptakes adoubleas its operand, so even if you dofloatmath for the rest, it’ll be promoted to adoubleanyway. Also note that conversions frominttofloat(and back) can be fairly slow.If you’re really only dealing with a half dozen variables or so, you’re almost certainly best off leaving them as
doubleand being done with it. Converting to a combination offloatandlongwill save about 14 bytes of data storage, but then add (around) 14 bytes of extra instructions to handle all the conversions betweenint,float, anddoubleat the right times, so you’ll end up with slower code that uses just as much memory anyway.