View Single Post
Old 05-06-2023, 04:27 PM   #613
chaley
Grand Sorcerer
chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.chaley ought to be getting tired of karma fortunes by now.
 
Posts: 12,450
Karma: 8012886
Join Date: Jan 2010
Location: Notts, England
Device: Kobo Libra 2
Quote:
Originally Posted by ownedbycats View Post
Mostly out of curiosity about how something works again.

program: $#timesread + 1
This returns '1'

program: add($#timesread, 1)
This returns '1.0'

Why are they different?

(also just realized I should be using $$, oops...)
Because the add() function was defined forever ago to always return a float. This can't change without potentially breaking templates.

The add operator (+) checks the result and converts the float to an int if the fractional part is zero. One can argue that defining it this way was wrong, but so be it.

Before you ask: no, I am not going to change the behavior of either one.

Note that the results are arithmetically identical.

If you don't like the ".0" then use round(), floor(), ceil(), fractional_part(), or format_number() to convert it. Or use re() to strip off the .0, as in
Code:
re(add(3), '^(.*)\.0$', '\1')
chaley is offline   Reply With Quote