* How is math-round different from other Calc functions?
@ 2022-02-01 21:12 Daniele Nicolodi
2022-02-02 8:29 ` Andreas Schwab
2022-02-02 8:34 ` Mattias Engdegård
0 siblings, 2 replies; 4+ messages in thread
From: Daniele Nicolodi @ 2022-02-01 21:12 UTC (permalink / raw)
To: Emacs developers
Hello,
it is possible to invoke Calc functions on quantities with units:
(calc-eval "sqrt(4.0 g)")
"2. sqrt(g)"
However, it this does not work for round():
(calc-eval "round(4.0 g)")
"round(4. g)"
Not knowing what is going on, I decided to build my own Calc function
that does what is required:
(defmath uround (x)
(* (round (remove-units x)) (extract-units x)))
However, this somehow does not work:
(calc-eval "uround(2.345 g)")
"uround(2.345 g)"
but it does if I replace round() with any other function:
(defmath uplus (x)
(* (1+ (remove-units x)) (calc-eval "kg" 'raw)))
(calc-eval "uplus(2.345 g)")
"3.345 g"
Why is it so? How do I work around this?
Thank you.
Cheers,
Dan
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: How is math-round different from other Calc functions?
2022-02-01 21:12 How is math-round different from other Calc functions? Daniele Nicolodi
@ 2022-02-02 8:29 ` Andreas Schwab
2022-02-02 8:34 ` Mattias Engdegård
1 sibling, 0 replies; 4+ messages in thread
From: Andreas Schwab @ 2022-02-02 8:29 UTC (permalink / raw)
To: Daniele Nicolodi; +Cc: Emacs developers
On Feb 01 2022, Daniele Nicolodi wrote:
> it is possible to invoke Calc functions on quantities with units:
>
> (calc-eval "sqrt(4.0 g)")
> "2. sqrt(g)"
>
> However, it this does not work for round():
>
> (calc-eval "round(4.0 g)")
> "round(4. g)"
What do you expect to happen?
--
Andreas Schwab, schwab@linux-m68k.org
GPG Key fingerprint = 7578 EB47 D4E5 4D69 2510 2552 DF73 E780 A9DA AEC1
"And now for something completely different."
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: How is math-round different from other Calc functions?
2022-02-01 21:12 How is math-round different from other Calc functions? Daniele Nicolodi
2022-02-02 8:29 ` Andreas Schwab
@ 2022-02-02 8:34 ` Mattias Engdegård
2022-02-02 8:51 ` Daniele Nicolodi
1 sibling, 1 reply; 4+ messages in thread
From: Mattias Engdegård @ 2022-02-02 8:34 UTC (permalink / raw)
To: Daniele Nicolodi; +Cc: Emacs developers
1 feb. 2022 kl. 22.12 skrev Daniele Nicolodi <daniele@grinta.net>:
> (defmath uround (x)
> (* (round (remove-units x)) (extract-units x)))
Looks like you may need to do `math-simplify` after `remove-units`, since `math-round` only works if its argument is a value, not an unsimplified expression such as `(* (float 4 0) 1)`.
> but it does if I replace round() with any other function:
>
> (defmath uplus (x)
> (* (1+ (remove-units x)) (calc-eval "kg" 'raw)))
That `(1+ ...)` becomes `(math-add ... x)` and presumably `math-add` makes an effort to simplify its arguments for other reasons.
At least, this is from a cursory look at the code. Does that make sense?
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: How is math-round different from other Calc functions?
2022-02-02 8:34 ` Mattias Engdegård
@ 2022-02-02 8:51 ` Daniele Nicolodi
0 siblings, 0 replies; 4+ messages in thread
From: Daniele Nicolodi @ 2022-02-02 8:51 UTC (permalink / raw)
To: Mattias Engdegård; +Cc: Emacs developers
On 02/02/2022 09:34, Mattias Engdegård wrote:
> 1 feb. 2022 kl. 22.12 skrev Daniele Nicolodi <daniele@grinta.net>:
>
>> (defmath uround (x)
>> (* (round (remove-units x)) (extract-units x)))
>
> Looks like you may need to do `math-simplify` after `remove-units`, since `math-round` only works if its argument is a value, not an unsimplified expression such as `(* (float 4 0) 1)`.
>
>> but it does if I replace round() with any other function:
>>
>> (defmath uplus (x)
>> (* (1+ (remove-units x)) (calc-eval "kg" 'raw)))
>
> That `(1+ ...)` becomes `(math-add ... x)` and presumably `math-add` makes an effort to simplify its arguments for other reasons.
>
> At least, this is from a cursory look at the code. Does that make sense?
Yes, that's it. Adding `usimplify()` I don't need to define a function
that explicitly deals with the units:
(calc-eval "usimplify(round(2.234 g))")
"2 g"
Thank you.
Cheers,
Dan
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2022-02-02 8:51 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2022-02-01 21:12 How is math-round different from other Calc functions? Daniele Nicolodi
2022-02-02 8:29 ` Andreas Schwab
2022-02-02 8:34 ` Mattias Engdegård
2022-02-02 8:51 ` Daniele Nicolodi
Code repositories for project(s) associated with this public inbox
https://git.savannah.gnu.org/cgit/emacs.git
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).