1000000 / tv96_to_usec(n)
As usual, Phil figures out the solution.
I actually need 1/msec precision, but I can get that, barely within 31 bits, like this:
1,000,000,000/tv96_to_usec(n) -- without the commas
My 1/3200 shutter time comes out to 1/3205 after the conversion. My old one came out to 1/3201.
I think there also is a precision problem with using (float) instead of (double) in this function:
float shooting_get_shutter_speed_from_tv96(short tv96)
{
return pow(2,((float)(-tv96))/96.0);
}
//how about changing it to:
double shooting_get_shutter_speed_from_tv96(short tv96)
{
return pow(2,((double)(-tv96))/96.0);
}
Search for the function in all source files. There's only a few places that need to be changed, i.e. cast to (float). Most of the time, the result is already cast to (int). Also, I think pow() expects a (double) 2nd argument, so the cast to float is cast back to (double) after the division, which wastes precision.
(float) only has about 6 or 7 digits of precision, so when you multiply a float by 1,000,000 you're not getting accurate microseconds for values over 1 second or so. Basically, you're computing values to put into 10 digit integers, using 7 digit floating point arithmetic. So it should be using double. For consistency, I would like to see all the conversion functions changed from float to double.
============
Here's how I modified my function to print shutter times using Phil's idea. I changed it so it doesn't start using 1/seconds format until it gets to 1/2 second, which matches what the camera displays and puts into the EXIF (i.e. 0.6 seconds instead of 1/1.66)
function seconds(tv) -- tv96 to time string i.e. print(seconds(tv))
local pre=""
if(tv>=96)then
tv=1000000000/tv96_to_usec(tv)
pre="1/"
else
tv=tv96_to_usec(tv)/1000
end
local ti=tv/1000 -- integer part
if(ti>1000)then ti=(ti/10)*10 end -- only 3 digits precision
local ts=string.format("%s%u.%03u",pre,ti,tv%1000) -- xx.xxx or 1/xx.xxx
if(ti>1)then -- truncate digits to reflect decimal precision
if(ti>149)then ti=-5
elseif(ti>14)then ti=-3
else ti=-2 end
ts=string.sub(ts,1,ti)
end
return ts
end