Message Boards Message Boards

7 Replies
9 Total Likes
View groups...
Share this post:

rounding with significant figures

Posted 12 years ago
I want to produce the standard range of camera f-stops in a tidy way in Mathematica, and this is pretty close:
In[29]:= Table[Floor[Sqrt[2^n], 0.1], {n, 1., 10.}]
Out[29]= {1.4, 2., 2.8, 4., 5.6, 8., 11.3, 16., 22.6, 32.}
However, I want the 11.3 and 22.6 to be just 11. and 22. In other words, I want floor with 2 significant figures. But how?

In this case the result is so tiny I could just enter the list directly, but I'd like to know the technique for future reference.
POSTED BY: Joel Klein
7 Replies
When a function is not available (like "floor"ing to precisely n digits), I find it cleanest to just write an implementation and wrap it up into a self contained function.
floorToNDigits[x_, n_] := N@Floor[x, 10^(Floor@Log[10, x] - n + 1)]
Table[floorToNDigits[Sqrt[2^n], 2], {n, 1, 10}]
(* ==>  {1.4, 2., 2.8, 4., 5.6, 8., 11., 16., 22., 32.} *)
Of course this will still not give the conventional values of three digits F-numbers, but are there any lenses at all that have those?  I assume diffraction would be a serious issue at 128.

EDIT: Found one.  Hopefuly it'll be possible to host images locally soon.

POSTED BY: Szabolcs Horvát
This works, at least in this case:
In[11]:= Table[Floor[Sqrt[2^n], 10.^(Floor[Log10[Sqrt[2^n]] - 1])], {n, 1., 10.}]
Out[11]= {1.4, 2., 2.8, 4., 5.6, 8., 11., 16., 22., 32.}
POSTED BY: Brett Champion
Table[SetPrecision[Floor[Sqrt[2^n], 0.1], 2], {n, 1.0, 10.0}]
I know that the function returns 23 instead of 22.  However, this is one of those cases where mathematics/physics meets the real world.  For one, those who came up with this scale realized that a precision of two digits is sufficient for the real world.  The use of SetPrecision is obviously part of the excercise.  A second issue is that it is not uncommon to make sure that no confusion arises.  Hence, in a series 11, 22, 32 it makes more sense to use the number 22 than to risk that somebody confuses 23 with 32.  This is one of many cases in which one requires knowledge while know-how does not suffice.
Ernst's looks good, but I got 23 instead of 22 when I ran it.
In[279]:= Table[SetPrecision[Floor[Sqrt[2^n], 0.1], 2], {n, 1.0, 10.0}]

Out[279]= {1.4, 2.0, 2.8, 4.0, 5.6, 8.0, 11., 16., 23., 32.}
POSTED BY: Jesse Friedman
Yes, Jesse, I also noticed this difference between the answers - obviously due to different round-up mechanisms used. But doesn't your answer also give 23? - It did for me when I ran your code. Just in case for everyone's information, here is the Standard full-stop f-number scale  

POSTED BY: Vitaliy Kaurov
Interesting, I didn't notice that. I used the same formula on the Wikipedia page.
POSTED BY: Jesse Friedman
I'm not sure I entirely understand the question, but does this solve the problem?
In[252]:= Table[Floor[Floor[Sqrt[2^n], 0.1]], {n, 1., 10.}]
Out[252]= {1, 2, 2, 4, 5, 8, 11, 16, 22, 32}

UPDATE: I read the Wikipedia page on f-stops, and I think I now understand the problem. Does this work?
Grid[{{"AV"}~Join~Table[av, {av, 10}], {"f/No."}~Join~
Table[N[Sqrt[2]^av, 2], {av, 10}]}, Frame -> All]

(It doesn't use Floor, but it works.)

POSTED BY: Jesse Friedman
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
or Discard

Group Abstract Group Abstract