Re: Supporting fractional values for a unit converter function

[email protected]
Newsgroups gmane.emacs.help
Message-ID <[email protected]>
Manuel Giraud <[email protected]> writes:

> Heime <[email protected]> writes:
>
>> On Sunday, March 22nd, 2026 at 2:05 AM, Heime <[email protected]> wrote:
>>
>>> I want to extend a unit conversion framework that accepts fractional
>>> inches by letting value be a Lisp rational (like 7/64) and then treating
>>> it as a normal number in the conversion.
>>> 
>>> To write (units 7/64 :inch :mm) rather than the cumbersome
>>> (units ((float 7) / (float 64)) :inch :mm)
>>
>> Can a function be made to take an argument 7/64 and interpret 
>> it inside the function as (/ (float 7) (float 64))?
>
> For this a user have to write (/ 7.0 64) as input.  See 3rd paragraph of
> the info node "(elisp) Float Basics".

Also, unlike many other Lisps, Emacs Lisp does not provide
rational numbers.

See (info "(elisp) Numbers")

  "GNU Emacs supports two numeric data types: “integers” and
   “floating-point numbers”.  Integers are whole numbers
   such as −3, 0, 7, 13, and 511.  Floating-point numbers
   are numbers with fractional parts, such as −4.5, 0.0, and
   2.71828."

In Emacs Lisp, an expression that looks like a rational
number, such as 7/64, will be interpreted as the name of a
symbol, which will either be unbound (likely) or will
reference a variable.  Try:

   (let ((7/16 "My unexpected variable’s value."))
     (message "%s" 7/16))
   ;;=> "My unexpected variable’s value."

   7/16
   ;;=> Debugger: (void-variable 7/16)

-- 
The lyf so short, the craft so long to lerne.
- Geoffrey Chaucer, The Parliament of Birds.
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.