Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
cmucl
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Carl Shapiro
cmucl
Commits
457085b8
Commit
457085b8
authored
31 years ago
by
ram
Browse files
Options
Downloads
Patches
Plain Diff
Added FLOAT-RATIO for precise ratio floating (as need by the float reader.)
parent
5cc1ce28
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
code/float.lisp
+67
-6
67 additions, 6 deletions
code/float.lisp
with
67 additions
and
6 deletions
code/float.lisp
+
67
−
6
View file @
457085b8
...
...
@@ -7,7 +7,7 @@
;;; Scott Fahlman or slisp-group@cs.cmu.edu.
;;;
(
ext:file-comment
"$Header: /Volumes/share2/src/cmucl/cvs2git/cvsroot/src/code/float.lisp,v 1.1
0
199
2/12/10 01:28:2
2 ram Exp $"
)
"$Header: /Volumes/share2/src/cmucl/cvs2git/cvsroot/src/code/float.lisp,v 1.1
1
199
3/05/07 12:10:5
2 ram Exp $"
)
;;;
;;; **********************************************************************
;;;
...
...
@@ -653,15 +653,76 @@
((
bignum
)
(
bignum-to-float
x
',type
))
((
ratio
)
(
let
((
num
(
numerator
x
))
(
den
(
denominator
x
)))
(
if
(
and
(
fixnump
num
)
(
fixnump
den
))
(
/
(
coerce
num
',type
)
(
coerce
den
',type
))
(
float-bignum-ratio
x
',type
))))))))
(
float-ratio
x
',type
))))))
(
frob
%single-float
single-float
)
(
frob
%double-float
double-float
))
;;; FLOAT-RATIO -- Internal
;;;
;;; Convert a ratio to a float. We avoid any rounding error by doing an
;;; integer division. Accuracy is important to preserve read/print
;;; consistency, since this is ultimately how the reader reads a float. We
;;; scale the numerator by a power of two until the division results in the
;;; desired number of fraction bits, then do round-to-nearest.
;;;
(
defun
float-ratio
(
x
format
)
(
let*
((
signed-num
(
numerator
x
))
(
plusp
(
plusp
signed-num
))
(
num
(
if
plusp
signed-num
(
-
signed-num
)))
(
den
(
denominator
x
))
(
digits
(
float-format-digits
format
))
(
scale
0
))
(
declare
(
fixnum
digits
scale
))
;;
;; Strip any trailing zeros from the denominator and move it into the scale
;; factor (to minimize the size of the operands.)
(
let
((
den-twos
(
1-
(
integer-length
(
logxor
den
(
1-
den
))))))
(
declare
(
fixnum
den-twos
))
(
decf
scale
den-twos
)
(
setq
den
(
ash
den
(
-
den-twos
))))
;;
;; Guess how much we need to scale by from the magnitudes of the numerator
;; and denominator. We want one extra bit for a guard bit.
(
let*
((
num-len
(
integer-length
num
))
(
den-len
(
integer-length
den
))
(
delta
(
-
den-len
num-len
))
(
shift
(
1+
(
the
fixnum
(
+
delta
digits
))))
(
shifted-num
(
ash
num
shift
)))
(
declare
(
fixnum
delta
shift
))
(
decf
scale
delta
)
(
labels
((
float-and-scale
(
bits
)
(
let*
((
bits
(
ash
bits
-1
))
(
len
(
integer-length
bits
)))
(
cond
((
>
len
digits
)
(
assert
(
=
len
(
the
fixnum
(
1+
digits
))))
(
scale-float
(
floatit
(
ash
bits
-1
))
(
1+
scale
)))
(
t
(
scale-float
(
floatit
bits
)
scale
)))))
(
floatit
(
bits
)
(
let
((
sign
(
if
plusp
0
1
)))
(
if
(
eq
format
'single-float
)
(
single-from-bits
sign
vm:single-float-bias
bits
)
(
double-from-bits
sign
vm:double-float-bias
bits
)))))
(
loop
(
multiple-value-bind
(
fraction-and-guard
rem
)
(
truncate
shifted-num
den
)
(
let
((
extra
(
-
(
integer-length
fraction-and-guard
)
digits
)))
(
declare
(
fixnum
extra
))
(
cond
((
/=
extra
1
)
(
assert
(
>
extra
1
)))
((
oddp
fraction-and-guard
)
(
if
(
zerop
rem
)
(
float-and-scale
(
if
(
zerop
(
logand
fraction-and-guard
2
))
fraction-and-guard
(
1+
fraction-and-guard
)))
(
return
(
float-and-scale
(
1+
fraction-and-guard
)))))
(
t
(
return
(
float-and-scale
fraction-and-guard
)))))
(
setq
shifted-num
(
ash
shifted-num
-1
))
(
incf
scale
)))))))
#|
These might be useful if we ever have a machine w/o float/integer conversion
hardware. For now, we'll use special ops that uninterruptibly frob the
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment