[Openmcl-devel] Type-of and positive/negative integers

Daniel Kochmański daniel at turtleware.eu
Thu Oct 24 01:08:14 PDT 2019


Hmm, maybe something in this spirit?

(let ((type (type-of x)))
  (flet ((maximize-type (new-number)
           (let ((new-type (type-of new-number)))
             (cond ((subtypep new-type type)
                    t)
                   ((subtypep type new-type)
                    (setf type new-type)
                    t)))))
     (assert (every #'maximize-type coefficients))))

Daniel

Steven Nunez writes:

>  Okay, having a specialised representation for positive integers makes sense as a further specialisation of INTEGER. I wish this was documented somewhere, a 1 minute google search did not turn up anything.
> I still have the problem of ensuring that the parameters to a function, a sequence and a number, are all of the same type so I can dispatch on specialised (typed declared) versions of loop. At the moment I have:
>  (assert (and (plusp (length coefficients))
>    (every (lambda (elt)
>      (typep elt (type-of x)))
>     coefficients)))
> which works fine for single-float and double-float, but fails if any of the coefficients are negative because their type-of is FIXNUM whilst the X or other coefficients may be (INTEGER 0 4611686018427387903)
> Any ideas?
>
>
>
>
>
>     On Thursday, October 24, 2019, 3:24:56 PM GMT+8, Daniel Kochmański <daniel at turtleware.eu> wrote:  
>  
>  
>> CCL (1.11) has most-positive-fixnum of 1152921504606846975SBCL (1.5.6) has 4611686018427387903
>>
>> However:
>> CL-USER> (subtypep 'fixnum 'integer)
>> T
>> T
>> CL-USER>
>>
>> so FIXNUM is a subtype of INTEGER, and it should be the type returned if the implementation is following the CltL2 guidelines if I'm understanding the type system correctly.
>
> CLtL2 has been superseded by ANSI specification so it is not
> bounding. Anyway, more specific type here is
>
>  (integer 0 #.most-positive-fixnum)
>
> Try (subtypep (integer 0 #.most-positive-fixnum) 'fixnum) and it will
> yield true as well.
>
> The reason why you have result 'fixnum for negative and '(integer 0
> #.most-positive-fixnum) for positive integers is probably because CCL
> (and other implementations in fact) have a specialized representation
> for unsinged fixnums (similar to i.e 'unsigned int' in C).
>
> Daniel


-- 
Daniel Kochmański ;; aka jackdaniel | Przemyśl, Poland
TurtleWare - Daniel Kochmański      | www.turtleware.eu

"Be the change that you wish to see in the world." - Mahatma Gandhi



More information about the Openmcl-devel mailing list