Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix int/float conversion logic #1004

Merged
merged 1 commit into from
Oct 5, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions library/math/conversions.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ cannot be represented in :TO. These fall into a few categories:
(define-instance (Into ,integer ,coalton-float)
(define (into x)
(lisp ,coalton-float (x)
(cl:coerce x (cl:quote ,lisp-float)))))))
(cl:coerce x ',lisp-float))))))

;; Only exact conversions
;; Single-Float: 24 bit mantissa (not including sign)
Expand Down Expand Up @@ -187,12 +187,12 @@ cannot be represented in :TO. These fall into a few categories:
`(define-instance (TryInto ,integer ,float)
(define (tryInto x)
(lisp (Result String ,float) (x)
(cl:if (cl:and (cl:> x (cl:expt -2 ,pow)) (cl:< x (cl:expt 2 ,pow)))
(cl:let ((y (cl:coerce x ',lisp-float)))
(cl:if (cl:< ,(cl:- (cl:expt 2 pow)) x ,(cl:expt 2 pow))
(cl:let ((y (cl:ignore-errors (cl:coerce x ',lisp-float))))
(cl:if (cl:null y)
(coalton-impl/util:unreachable)
(coalton-impl/util:unreachable)
(Ok y)))
(Err `,(cl:concatenate 'cl:string "Given integer" "is not within " "(-2^" (cl:write-to-string ,pow) ", " "2^" (cl:write-to-string ,pow) ")"))))))))
(Err ,(cl:format cl:nil "Given integer is not within (-2^~D, 2^~D)." pow pow))))))))

(coalton-toplevel
;; Single Float
Expand All @@ -208,7 +208,7 @@ cannot be represented in :TO. These fall into a few categories:

(integer-tryinto-float I32 cl:single-float Single-Float 24)

;; Double Float
;; Double Float
(integer-tryinto-float I64 cl:double-float Double-Float 53)

(integer-tryinto-float U64 cl:double-float Double-Float 53)
Expand Down