Translation

(itstool) path: step/para
Initialize a counter to <constant>0</constant>.
43/470
Context English Persian State
That still leaves one possibility uncovered: If all the user enters is a zero (or several zeros), we will never find a non–zero to display.
We can determine this has happened whenever our counter stays at <constant>0</constant>. In that case we need to send <constant>0</constant> to the output, and perform another "slap in the face":
0 ??? ??? ??? ??? ??? 0 ??? ??? ??? ??? ???
Once we have displayed the focal length and determined it is valid (greater than <constant>0</constant> but not exceeding 18 digits), we can calculate the pinhole diameter.
It is not by coincidence that <emphasis>pinhole</emphasis> contains the word <emphasis>pin</emphasis>. Indeed, many a pinhole literally is a <emphasis>pin hole</emphasis>, a hole carefully punched with the tip of a pin.
That is because a typical pinhole is very small. Our formula gets the result in millimeters. We will multiply it by <constant>1000</constant>, so we can output the result in <emphasis>microns</emphasis>.
At this point we have yet another trap to face: <emphasis>Too much precision.</emphasis>
Yes, the <acronym>FPU</acronym> was designed for high precision mathematics. But we are not dealing with high precision mathematics. We are dealing with physics (optics, specifically).
Suppose we want to convert a truck into a pinhole camera (we would not be the first ones to do that!). Suppose its box is <constant>12</constant> meters long, so we have the focal length of <constant>12000</constant>. Well, using Bender's constant, it gives us square root of <constant>12000</constant> multiplied by <constant>0.04</constant>, which is <constant>4.381780460</constant> millimeters, or <constant>4381.780460</constant> microns.
Put either way, the result is absurdly precise. Our truck is not <emphasis>exactly</emphasis> <constant>12000</constant> millimeters long. We did not measure its length with such a precision, so stating we need a pinhole with the diameter of <constant>4.381780460</constant> millimeters is, well, deceiving. <constant>4.4</constant> millimeters would do just fine.
I "only" used ten digits in the above example. Imagine the absurdity of going for all 18!
We need to limit the number of significant digits of our result. One way of doing it is by using an integer representing microns. So, our truck would need a pinhole with the diameter of <constant>4382</constant> microns. Looking at that number, we still decide that <constant>4400</constant> microns, or <constant>4.4</constant> millimeters is close enough.
Additionally, we can decide that no matter how big a result we get, we only want to display four significant digits (or any other number of them, of course). Alas, the <acronym>FPU</acronym> does not offer rounding to a specific number of digits (after all, it does not view the numbers as decimal but as binary).
We, therefore, must devise an algorithm to reduce the number of significant digits.
Here is mine (I think it is awkward—if you know a better one, <emphasis>please</emphasis>, let me know):
Initialize a counter to <constant>0</constant>. مقداردهی شمارنده با <constant>0</constant>.
While the number is greater than or equal to <constant>10000</constant>, divide it by <constant>10</constant> and increase the counter. در حالی که عدد بزرگ‌تر یا مساوی با <constant>10000</constant> باشد، تقسیم آن بر <constant>10</constant> و افزایش شمارنده.
Output the result. نمایش نتیجه.
While the counter is greater than <constant>0</constant>, output <constant>0</constant> and decrease the counter. در حالی که شمارنده بزرگ‌تر از <constant>0</constant> است، نمایش <constant>0</constant> و کاهش شمارنده.
The <constant>10000</constant> is only good if you want <emphasis>four</emphasis> significant digits. For any other number of significant digits, replace <constant>10000</constant> with <constant>10</constant> raised to the number of significant digits.
We will, then, output the pinhole diameter in microns, rounded off to four significant digits.
At this point, we know the <emphasis>focal length</emphasis> and the <emphasis>pinhole diameter</emphasis>. That means we have enough information to also calculate the <emphasis>f–number</emphasis>.
We will display the f–number, rounded to four significant digits. Chances are the f–number will tell us very little. To make it more meaningful, we can find the nearest <emphasis>normalized f–number</emphasis>, i.e., the nearest power of the square root of 2.
We do that by multiplying the actual f–number by itself, which, of course, will give us its <function>square</function>. We will then calculate its base–2 logarithm, which is much easier to do than calculating the base–square–root–of–2 logarithm! We will round the result to the nearest integer. Next, we will raise 2 to the result. Actually, the <acronym>FPU</acronym> gives us a good shortcut to do that: We can use the <function role="opcode">fscale</function> op code to "scale" 1, which is analogous to <function role="opcode">shift</function>ing an integer left. Finally, we calculate the square root of it all, and we have the nearest normalized f–number.
If all that sounds overwhelming—or too much work, perhaps—it may become much clearer if you see the code. It takes 9 op codes altogether:
fmul st0, st0
fld1
fld st1
fyl2x
frndint
fld1
fscale
fsqrt
fstp st1
fmul st0, st0
fld1
fld st1
fyl2x
frndint
fld1
fscale
fsqrt
fstp st1
The first line, <function role="opcode">fmul st0, st0</function>, squares the contents of the <acronym>TOS</acronym> (top of the stack, same as <varname role="register">st</varname>, called <varname role="register">st0</varname> by <application>nasm</application>). The <function role="opcode">fld1</function> pushes <constant>1</constant> on the <acronym>TOS</acronym>.
The next line, <function role="opcode">fld st1</function>, pushes the square back to the <acronym>TOS</acronym>. At this point the square is both in <varname role="register">st</varname> and <varname role="register">st(2)</varname> (it will become clear why we leave a second copy on the stack in a moment). <varname role="register">st(1)</varname> contains <constant>1</constant>.
Next, <function role="opcode">fyl2x</function> calculates base–2 logarithm of <varname role="register">st</varname> multiplied by <varname role="register">st(1)</varname>. That is why we placed <constant>1</constant> on <varname role="register">st(1)</varname> before.
At this point, <varname role="register">st</varname> contains the logarithm we have just calculated, <varname role="register">st(1)</varname> contains the square of the actual f–number we saved for later.
<function role="opcode">frndint</function> rounds the <acronym>TOS</acronym> to the nearest integer. <function role="opcode">fld1</function> pushes a <constant>1</constant>. <function role="opcode">fscale</function> shifts the <constant>1</constant> we have on the <acronym>TOS</acronym> by the value in <varname role="register">st(1)</varname>, effectively raising 2 to <varname role="register">st(1)</varname>.

Loading…

Initialize a counter to <constant>0</constant>.
مقداردهی شمارنده با <constant>0</constant>.
3 months ago
Browse all component changes

Glossary

English Persian
No related strings found in the glossary.

Source information

Source string comment
(itstool) path: step/para
Source string location
book.translate.xml:13424
String age
6 months ago
Source string age
a year ago
Translation file
books/fa/developers-handbook.po, string 2112