float.elc 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. (defconst exp-base 2 "\
  2. Base of exponent in this floating point representation.")
  3. (defconst mantissa-bits 24 "\
  4. Number of significant bits in this floating point representation.")
  5. (defconst decimal-digits 6 "\
  6. Number of decimal digits expected to be accurate.")
  7. (defconst expt-digits 2 "\
  8. Maximum permitted digits in a scientific notation exponent.")
  9. (defconst maxbit (1- mantissa-bits) "\
  10. Number of highest bit")
  11. (defconst mantissa-maxval (1- (ash 1 maxbit)) "\
  12. Maximum permissable value of mantissa")
  13. (defconst mantissa-minval (ash 1 maxbit) "\
  14. Minimum permissable value of mantissa")
  15. (defconst floating-point-regexp "^[ ]*\\(-?\\)\\([0-9]*\\)\\(\\.\\([0-9]*\\)\\|\\)\\(\\(\\([Ee]\\)\\(-?\\)\\([0-9][0-9]*\\)\\)\\|\\)[ ]*$" "\
  16. Regular expression to match floating point numbers. Extract matches:
  17. 1 - minus sign
  18. 2 - integer part
  19. 4 - fractional part
  20. 8 - minus sign for power of ten
  21. 9 - power of ten
  22. ")
  23. (defconst high-bit-mask (ash 1 maxbit) "\
  24. Masks all bits except the high-order (sign) bit.")
  25. (defconst second-bit-mask (ash 1 (1- maxbit)) "\
  26. Masks all bits except the highest-order magnitude bit")
  27. (setq _f0 (quote (0 . 1)))
  28. (setq _f1/2 (quote (4194304 . -23)))
  29. (setq _f1 (quote (4194304 . -22)))
  30. (setq _f10 (quote (5242880 . -19)))
  31. (setq powers-of-10 (make-vector (1+ decimal-digits) _f1))
  32. (aset powers-of-10 1 _f10)
  33. (aset powers-of-10 2 (quote (6553600 . -16)))
  34. (aset powers-of-10 3 (quote (8192000 . -13)))
  35. (aset powers-of-10 4 (quote (5120000 . -9)))
  36. (aset powers-of-10 5 (quote (6400000 . -6)))
  37. (aset powers-of-10 6 (quote (8000000 . -3)))
  38. (setq all-decimal-digs-minval (aref powers-of-10 (1- decimal-digits)) highest-power-of-10 (aref powers-of-10 decimal-digits))
  39. (defun fashl (fnum) (byte-code "Á@Â\"ASB‡" [fnum ash 1] 3))
  40. (defun fashr (fnum) (byte-code "Á@Â\"ATB‡" [fnum ash -1] 3))
  41. (defun normalize (fnum) (byte-code "@ÄVƒÅÆ@ \"!…Ç!‰ˆ‚‚=@ÄWƒ:ÅÆ@
  42. \"!…7Ç!‰ˆ‚$‚= ‰ˆ‡" [fnum second-bit-mask high-bit-mask _f0 0 zerop logand fashl] 9))
  43. (defun abs (n) (byte-code "Á!ƒ
  44. ‚ [‡" [n natnump] 2))
  45. (defun fabs (fnum) (byte-code "ÁÂ@!AB!‡" [fnum normalize abs] 4))
  46. (defun xor (a b) (byte-code "† …… ?‡" [a b] 1))
  47. (defun same-sign (a b) (byte-code "ÂÃ@!Ã @!\"?‡" [a b xor natnump] 5))
  48. (defun extract-match (str i) (byte-code "ÀÃÄ�‡" [nil str i (byte-code " !à !O‡" [str i match-beginning match-end] 5) ((error (byte-code "À‡" [""] 1)))] 3))
  49. (setq halfword-bits (/ mantissa-bits 2) masklo (1- (ash 1 halfword-bits)) maskhi (lognot masklo) round-limit (ash 1 (/ halfword-bits 2)))
  50. (defun hihalf (n) (byte-code "ÃÄ \"
  51. [\"‡" [n maskhi halfword-bits ash logand] 4))
  52. (defun lohalf (n) (byte-code "Â \"‡" [n masklo logand] 3))
  53. (defun f+ (a1 a2) "\
  54. Returns the sum of two floating point numbers." (byte-code "Ä
  55. \"Å
  56. \"Æ
  57. \"ƒÇ!Ç !‰‚ȈÉ@Ê @ AAZ\"\\AB!*‡" [f1 a1 a2 f2 fmax fmin same-sign fashr nil normalize ash] 11))
  58. (defun f- (a1 &optional a2) "\
  59. Returns the difference of two floating point numbers." (byte-code "ƒ Â Ã!\"‚Ä @[ AB!‡" [a2 a1 f+ f- normalize] 5))
  60. (defun f* (a1 a2) "\
  61. Returns the product of two floating point numbers." (byte-code "É !@É !@Ê \"?ËÌÍÎ!Î
  62. !\"!ÎÍÌ!Î
  63. !\"!ÎÍÎ!Ì
  64. !\"!#ËÍÌ!Ì
  65. !\"ÌÍÌ!Î
  66. !\"!ÌÍÎ!Ì
  67. !\"!Ì !$Î !VƒfT‰‚gψРƒs[‚uËÉ !AÉ !A#B!-‡" [i1 a1 i2 a2 sign prodlo prodhi round-limit mantissa-bits fabs same-sign + hihalf * lohalf nil normalize] 38))
  68. (defun f/ (a1 a2) "\
  69. Returns the quotient of two floating point numbers." (byte-code "È@!ƒÉÊË E\"‚v SÌÍ !@Í!@Î \"?Ï
  70. !…Z ZÌWƒ@Ð Ñ\"‰‚LÐ Ñ\"T Z‰ˆÐ Ñ\"
  71. S‰ˆ‚(ˆÒƒf [‚g ÓÍ !AÍ!A S#B!-‡" [a2 a1 bits maxbit quotient dividend divisor sign zerop signal arith-error "attempt to divide by zero" 0 fabs same-sign natnump ash 1 normalize -] 17))
  72. (defun f% (a1 a2) "\
  73. Returns the remainder of first floating point number divided by second." (byte-code "ÂÃÄÅ \"! \"\"‡" [a1 a2 f- f* ftrunc f/] 7))
  74. (defun f= (a1 a2) "\
  75. Returns t if two floating point numbers are equal, nil otherwise." (byte-code "Â \"‡" [a1 a2 equal] 3))
  76. (defun f> (a1 a2) "\
  77. Returns t if first floating point number is greater than second,
  78. nil otherwise." (byte-code "Ä@!… @ÅWƒ‚P@ÅV… @ÅXƒ$‚P@ÅX…/Ä @!ƒ6ÂPÆA A\"ƒGA AV‚PÂ…P@ @V‡" [a1 a2 t nil natnump 0 /=] 5))
  79. (defun f>= (a1 a2) "\
  80. Returns t if first floating point number is greater than or equal to
  81. second, nil otherwise." (byte-code "Â \"† Ã \"‡" [a1 a2 f> f=] 4))
  82. (defun f< (a1 a2) "\
  83. Returns t if first floating point number is less than second,
  84. nil otherwise." (byte-code "Â \"?‡" [a1 a2 f>=] 3))
  85. (defun f<= (a1 a2) "\
  86. Returns t if first floating point number is less than or equal to
  87. second, nil otherwise." (byte-code "Â \"?‡" [a1 a2 f>] 3))
  88. (defun f/= (a1 a2) "\
  89. Returns t if first floating point number is not equal to second,
  90. nil otherwise." (byte-code "Â \"?‡" [a1 a2 f=] 3))
  91. (defun fmin (a1 a2) "\
  92. Returns the minimum of two floating point numbers." (byte-code "Â \"ƒ ‚ ‡" [a1 a2 f<] 3))
  93. (defun fmax (a1 a2) "\
  94. Returns the maximum of two floating point numbers." (byte-code "Â \"ƒ ‚ ‡" [a1 a2 f>] 3))
  95. (defun fzerop (fnum) "\
  96. Returns t if the floating point number is zero, nil otherwise." (byte-code "@ÁU‡" [fnum 0] 2))
  97. (defun floatp (fnum) "\
  98. Returns t if the arg is a floating point number, nil otherwise." (byte-code ":…Á@!…ÁA!‡" [fnum integerp] 3))
  99. (defun f (int) "\
  100. Convert the integer argument to floating point, like a C cast operator." (byte-code "ÁÂB!‡" [int normalize 0] 3))
  101. (defun int-to-hex-string (int) "\
  102. Convert the integer argument to a C-style hexadecimal string." (byte-code "ÄÅÆÇX…# È
  103. ÉÊ \"Ë\"H!PÌ\\‰ˆ‚ˆ +‡" [shiftval str hex-chars int -20 "0x" "0123456789ABCDEF" 0 char-to-string logand lsh 15 4] 8))
  104. (defun ftrunc (fnum) "\
  105. Truncate the fractional part of a floating point number." (byte-code "ÅA!ƒ ‚AA [XƒÆ‚AÂ…A@AÇÅ !ƒ3ÈÈ \" [\"‚=ÈÈ [ \" [\"[ B!*‡" [fnum maxbit t mant exp natnump (0 . 1) normalize ash] 9))
  106. (defun fint (fnum) "\
  107. Convert the floating point number to integer, with truncation,
  108. like a C cast operator." (byte-code "È !@A Yƒ ‚( [Xƒ ‚(Ç…(É
  109. \"+‡" [tf fnum tint texp mantissa-bits mantissa-maxval mantissa-minval t ftrunc ash] 4))
  110. (defun float-to-string (fnum &optional sci) "\
  111. Convert the floating point number to a decimal string.
  112. Optional second argument non-nil means use scientific notation." (byte-code "Ò ! @ÓWÓÓÔÓÕ  \"ƒ\"Ö‚T×\"ƒfØÙ
  113. \"‰\"…G  \\‰ˆ‚*ˆØÙ \"‰\"…c T‰ˆ‚H‚œÚÛ
  114. \"‰\"…ƒ  Z‰ˆ‚fˆÚ\"…œÛ \" S‰ˆ‚„ˆÙÛ\" \"Ü!‰ˆÚÝ \"\"ƒÃÞß !T!‰‚ÊÞß !!‰ˆƒäà ÓáOâ áãOäÞ !%‰‚H  SYƒ   Zå!… ÖPS‰ˆ‚ò)‚H ÓWƒ6 [æZå!…,Ö PS‰ˆ‚ˆç P‰)‚HÑ…H Ó TOâ TãOQ‰ˆ
  115. ƒSè P‚T .‡" [value fnum sign power result str temp pow10 _f1 _f0 highest-power-of-10 decimal-digits _f10 all-decimal-digs-minval _f1/2 sci zeroes t fabs 0 "" f= "0" f>= f<= f* f> f/ ftrunc f- int-to-string fint concat 1 "." nil "E" natnump 2 "0." "-"] 29))
  116. (defun string-to-float (str) "\
  117. Convert the string to a floating point number.
  118. Accepts a decimal string in scientific notation,
  119. with exponent preceded by either E or e.
  120. Only the 6 most significant digits of the integer and fractional parts
  121. are used; only the first two digits of the exponent are used.
  122. Negative signs preceding both the decimal number and the exponent
  123. are recognized." (byte-code "× Ø#ƒ-ÉÙÚ Û\"Ú Ü\" PÝÚ Þ\"ß\"ØÉ G
  124. Z‰ˆ GW…> HàU…KT‰ˆ‚0ˆ
  125. Z ÉO‰ˆ G
  126. Vƒs 
  127. HáY Ø
  128. O‰‚|
  129. 
  130. GZ\\‰ˆâãä !ƒ‹Þ‚ŒØ\\ƒ–å‚—Þ\"!.Ú æ\" ÝÚ ç\"ß\" Ø ØØÙãä Ø G^O! ƒÐå‚ÑÞ\"
  131. \\‰ ˆ ØWƒë [ 艂ìɈé 
  132. \"ê 
  133. \"‰ˆØV…ë#S‰ˆ‚ÿˆëH#.\")‚/‡" [floating-point-regexp str power int-subst fract-subst digit-string mant-sign leading-0s round-up nil decimal-digits expt-subst expt-sign expt chunks tens exponent _f1 func expt-digits highest-power-of-10 powers-of-10 _f0 string-match 0 f* extract-match 2 4 equal 1 "-" 48 53 f * string-to-int -1 9 8 f/ / % funcall] 23))