math.3m
来自「<B>Digital的Unix操作系统VAX 4.2源码</B>」· 3M 代码 · 共 550 行 · 第 1/2 页
3M
550 行
.NE.RE\fBRounding:\fP.RSEvery algebraic operation (+, \-, \(**, /,.if n \sqrt).if t \\(sr)is rounded by default to within half an \*(up, andwhen the rounding error is exactly half an \*(up thenthe rounded value's least significant bit is zero.This kind of rounding is usually the best kind,sometimes provably so; for instance, for everyx = 1.0, 2.0, 3.0, 4.0, ..., 2.0**52, we find(x/3.0)\(**3.0 == x and (x/10.0)\(**10.0 == x and ...despite that both the quotients and the productshave been rounded. Only rounding like IEEE 754can do that. But no single kind of rounding can beproved best for every circumstance, so IEEE 754provides rounding towards zero or towards.If +or towards.If \-at the programmer's option. And thesame kinds of rounding are specified forBinary\-Decimal Conversions, at least for magnitudesbetween roughly 1.0e\-10 and 1.0e37..RE\fBExceptions:\fP.RSIEEE 754 recognizes five kinds of floating\-point exceptions,listed below in declining order of probable importance..PP.TStab(@);lfHB lfHBlfR lfR ._.sp 4pException@Default Result.sp 4p_.sp 6pInvalid Operation@\*(nn, or FALSE.if n \{\Overflow@\(+-InfinityDivide by Zero@\(+-Infinity \}.if t \{\Overflow@\(+-\(ifDivide by Zero@\(+-\(if \}Underflow@Gradual UnderflowInexact@Rounded value.sp 6p_.TE.PP.NTAn Exception is not an Error unless handledbadly. What makes a class of exceptions exceptionalis that no single default response can be satisfactoryin every instance. On the other hand, if a defaultresponse will serve most instances satisfactorily,the unsatisfactory instances cannot justify abortingcomputation every time the exception occurs..NE.RE.PPFor each kind of floating\-point exception, IEEE 754provides a Flag that is raised each time its exceptionis signaled, and stays raised until the program resetsit. Programs may also test, save and restore a flag.Thus, IEEE 754 provides three ways by which programsmay cope with exceptions for which the default resultmight be unsatisfactory:.IP 1) \w'\0\0\0\0'uTest for a condition that might cause an exceptionlater, and branch to avoid the exception..IP 2) \w'\0\0\0\0'uTest a flag to see whether an exception has occurredsince the program last reset its flag..IP 3) \w'\0\0\0\0'uTest a result to see whether it is a value that onlyan exception could have produced..NTThe only reliable ways to discoverwhether Underflow has occurred are to test whetherproducts or quotients lie closer to zero than theunderflow threshold, or to test the Underflowflag. (Sums and differences cannot underflow inIEEE 754; if x.if n \!=.if t \\(!=y then x\-y is correct tofull precision and certainly nonzero regardless ofhow tiny it may be.) Products and quotients thatunderflow gradually can lose accuracy graduallywithout vanishing, so comparing them with zero(as one might on a VAX) will not reveal the loss.Fortunately, if a gradually underflowed value isdestined to be added to something bigger than theunderflow threshold, as is almost always the case,digits lost to gradual underflow will not be missedbecause they would have been rounded off anyway.So gradual underflows are usually \fIprovably\fR ignorable.The same cannot be said of underflows flushed to 0..NE.PPAt the option of an implementor conforming to IEEE 754,other ways to cope with exceptions may be provided:.IP 4) \w'\0\0\0\0'uABORT. This mechanism classifies an exception inadvance as an incident to be handled by meanstraditionally associated with error\-handlingstatements like "ON ERROR GO TO ...". Differentlanguages offer different forms of this statement,but most share the following characteristics:.IP \(em \w'\0\0\0\0'uNo means is provided to substitute a value forthe offending operation's result and resumecomputation from what may be the middle of anexpression. An exceptional result is abandoned..IP \(em \w'\0\0\0\0'uIn a subprogram that lacks an error\-handlingstatement, an exception causes the subprogram toabort within whatever program called it, and soon back up the chain of calling subprograms untilan error\-handling statement is encountered or thewhole task is aborted and memory is dumped..IP 5) \w'\0\0\0\0'uSTOP. This mechanism, requiring an interactivedebugging environment, is more for the programmerthan the program. It classifies an exception inadvance as a symptom of a programmer's error; theexception suspends execution as near as it can tothe offending operation so that the programmer canlook around to see how it happened. Quite oftenthe first several exceptions turn out to be quiteunexceptionable, so the programmer ought ideallyto be able to resume execution after each one as ifexecution had not been stopped..IP 6) \w'\0\0\0\0'u\&... Other ways lie beyond the scope of this document..RE.PPThe crucial problem for exception handling is the problem ofScope, and the problem's solution is understood, but notenough manpower was available to implement it fully in timeto be distributed in 4.3 BSD's \fIlibm\fR. Ideally, eachelementary function should act as if it were indivisible, oratomic, in the sense that ....IP i) \w'iii)'u+2nNo exception should be signaled that is not deserved bythe data supplied to that function..IP ii) \w'iii)'u+2nAny exception signaled should be identified with thatfunction rather than with one of its subroutines..IP iii) \w'iii)'u+2nThe internal behavior of an atomic function should notbe disrupted when a calling program changes fromone to another of the five or so ways of handlingexceptions listed above, although the definitionof the function may be correlated intentionallywith exception handling..PPIdeally, every programmer should be able \fIconveniently\fR toturn a debugged subprogram into one that appears atomic toits users. But simulating all three characteristics of anatomic function is still a tedious affair, entailing hostsof tests and saves\-restores; work is under way to amelioratethe inconvenience..PPMeanwhile, the functions in \fIlibm\fR are only approximatelyatomic. They signal no inappropriate exception exceptpossibly ....RSOver/Underflow.RSwhen a result, if properly computed, might have lain barely within range, and.REInexact in \fIcabs\fR, \fIcbrt\fR, \fIhypot\fR, \fIlog10\fR and \fIpow\fR.RSwhen it happens to be exact, thanks to fortuitous cancellation of errors..RE.REOtherwise, ....RSInvalid Operation is signaled only when.RSany result but \*(nn would probably be misleading..REOverflow is signaled only when.RSthe exact result would be finite but beyond the overflow threshold..REDivide\-by\-Zero is signaled only when.RSa function takes exactly infinite values at finite operands..REUnderflow is signaled only when.RSthe exact result would be nonzero but tinier than the underflow threshold..REInexact is signaled only when.RSgreater range or precision would be needed to represent the exact result..RE.RE \fBExceptions on RISC machines:\fP.RSThe exception enables and the flags that are raised when an exceptionoccurs (as well as the rounding mode) are in the floating\-point control andstatus register. This register can be read or written by the routinesdescribed on the man page.IR fpc (3).This register's layout is described in the file.RI < mips/fpu.h >in UMIPS\-BSD releases and in.RI < sys/fpu.h >in UMIPS\-SYSV releases..sp +.5What is currently available is only the raw interface which was onlyintended to be used by the code to implement IEEE user trap handlers.IEEE floating\-point exceptions are enabled by setting the enable bitfor that exception in the floating\-point control and status register.If an exception then occurs the UNIX signal SIGFPE is sent to theprocess. It is up to the signal handler to determine the instructionthat caused the exception and to take the action specified by theuser. The instruction that caused the exception is in one of twoplaces. If the floating\-point board is used (the floating\-pointimplementation revision register indicates this in it's implementationfield) then the instruction that caused the exception is in thefloating\-point exception instruction register. In all otherimplementations the instruction that caused the exception is atthe address of the program counter as modified by the branch delaybit in the cause register. Both the program counter and cause registerare in the sigcontext structure passed to the signal handler (see.MS signal 3 ).If the program is to be continued past the instruction that causedthe exception the program counter in the signal context must beadvanced. If the instruction is in a branch delay slot then thebranch must be emulated to determine if the branch is taken andthen the resulting program counter can be calculated (see.MS emulate_branch 3and .MS signal 3 )..RE.SH RestrictionsWhen signals are appropriate, they are emitted by certainoperations within the codes, so a subroutine\-trace may beneeded to identify the function with its signal in casemethod 5) above is in use. And the codes all take theIEEE 754 defaults for granted; this means that a decision totrap all divisions by zero could disrupt a code that wouldotherwise get correct results despite division by zero..SH See Alsofpc(3), signal(3), emulate_branch(3).br.I "R2010 Floating Point Coprocessor Architecture".br.I "R2360 Floating Point Board Product Description".PPAn explanation of IEEE 754 and its proposed extension p854was published in the IEEE magazine MICRO in August 1984 underthe title "A Proposed Radix\- and Word\-length\-independentStandard for Floating\-point Arithmetic" by W. J. Cody et al..PPArticles in the IEEE magazine COMPUTER vol. 14 no. 3 (Mar.1981), and in the ACM SIGNUM Newsletter Special Issue ofOct. 1979, may be helpful although they pertain tosuperseded drafts of the standard.
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?