unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
* bug#59531: 29.0.50: An alternative to `string-to-number` which throws an error (or returns a NIL value) when input is non-parseable as number
@ 2022-11-24  6:14 Ramesh Nedunchezian
  2022-11-24  8:00 ` Eli Zaretskii
  2022-11-24 10:08 ` Jean Louis
  0 siblings, 2 replies; 5+ messages in thread
From: Ramesh Nedunchezian @ 2022-11-24  6:14 UTC (permalink / raw)
  To: 59531

`string-to-number` returns ZERO if the input is not a number.  


This return value is not very helpful.  The choice of a number ZERO as "Not A Number" doesn't help one to distinguish between the following two cases

(1) Input was a valid number, and it parses to number zero

(2) Input was NOT a valid number, and it was forcibly reported as ZERO

Consider amending `string-to-number` to throw an error (or return NIL) when the input is not parseable as a number, or providing an alternative API to validate numbers.  I am trying to parse some fields in an org table, and see if the field value is a number or not;

If there is already an alternative to what I am trying to accomplish, I would appreciate a recipe.








^ permalink raw reply	[flat|nested] 5+ messages in thread

* bug#59531: 29.0.50: An alternative to `string-to-number` which throws an error (or returns a NIL value) when input is non-parseable as number
  2022-11-24  6:14 bug#59531: 29.0.50: An alternative to `string-to-number` which throws an error (or returns a NIL value) when input is non-parseable as number Ramesh Nedunchezian
@ 2022-11-24  8:00 ` Eli Zaretskii
  2022-12-14 16:40   ` Robert Pluim
  2022-11-24 10:08 ` Jean Louis
  1 sibling, 1 reply; 5+ messages in thread
From: Eli Zaretskii @ 2022-11-24  8:00 UTC (permalink / raw)
  To: Ramesh Nedunchezian; +Cc: 59531

tags 59531 wishlist
thanks

> Date: Thu, 24 Nov 2022 11:44:43 +0530
> From: Ramesh Nedunchezian <rameshnedunchezian@outlook.com>
> 
> `string-to-number` returns ZERO if the input is not a number.  
> 
> 
> This return value is not very helpful.  The choice of a number ZERO as "Not A Number" doesn't help one to distinguish between the following two cases
> 
> (1) Input was a valid number, and it parses to number zero
> 
> (2) Input was NOT a valid number, and it was forcibly reported as ZERO
> 
> Consider amending `string-to-number` to throw an error (or return NIL) when the input is not parseable as a number, or providing an alternative API to validate numbers.  I am trying to parse some fields in an org table, and see if the field value is a number or not;

Thanks.

Changing the default behavior to signal an error is out of the question,
since this is used in the Lisp reader and elsewhere, all over the place.  It
is very useful there.

However, as an enhancement, we could have an additional optional argument to
request that the function signal an error if the string cannot be parsed as
a number.

> If there is already an alternative to what I am trying to accomplish, I would appreciate a recipe.

You could match the string to a regexp that validates its numerical
appearance, before calling string-to-number.





^ permalink raw reply	[flat|nested] 5+ messages in thread

* bug#59531: 29.0.50: An alternative to `string-to-number` which throws an error (or returns a NIL value) when input is non-parseable as number
  2022-11-24  6:14 bug#59531: 29.0.50: An alternative to `string-to-number` which throws an error (or returns a NIL value) when input is non-parseable as number Ramesh Nedunchezian
  2022-11-24  8:00 ` Eli Zaretskii
@ 2022-11-24 10:08 ` Jean Louis
  1 sibling, 0 replies; 5+ messages in thread
From: Jean Louis @ 2022-11-24 10:08 UTC (permalink / raw)
  To: Ramesh Nedunchezian; +Cc: 59531

* Ramesh Nedunchezian <rameshnedunchezian@outlook.com> [2022-11-24 09:22]:
> `string-to-number` returns ZERO if the input is not a number.  
> 
> 
> This return value is not very helpful.  The choice of a number ZERO as "Not A Number" doesn't help one to distinguish between the following two cases
> 
> (1) Input was a valid number, and it parses to number zero
> 
> (2) Input was NOT a valid number, and it was forcibly reported as ZERO
> 
> Consider amending `string-to-number` to throw an error (or return NIL) when the input is not parseable as a number, or providing an alternative API to validate numbers.  I am trying to parse some fields in an org table, and see if the field value is a number or not;
> 
> If there is already an alternative to what I am trying to
> accomplish, I would appreciate a recipe.

I had the same problem, so I have solved it this way.

(defun string-is-number-p (s)
  "Return number only if string is actual number, otherwise NIL."
  (let* ((s (string-trim s)))
    (cond ((seq-empty-p s) nil)
	  ((string-match "[^0123456789\\.-]" s) nil)
	  ((string-match "-" s 1) nil)
	  ((numberp (string-to-number s)) (string-to-number s)))))

As in my case I liketo know that string is really representing number
and nothing else.

(string-is-number-p "Hello") ➜ nil
(string-is-number-p "") ➜ nil
(string-is-number-p "0") ➜ 0
(string-is-number-p "0a") ➜ nil
(string-to-number "0a") ➜ 0
(string-is-number-p "0.1") ➜ 0.1
(string-is-number-p "-2.5121212") ➜ -2.5121212

My function may not be perfect.

--
Jean

Take action in Free Software Foundation campaigns:
https://www.fsf.org/campaigns

In support of Richard M. Stallman
https://stallmansupport.org/





^ permalink raw reply	[flat|nested] 5+ messages in thread

* bug#59531: 29.0.50: An alternative to `string-to-number` which throws an error (or returns a NIL value) when input is non-parseable as number
  2022-11-24  8:00 ` Eli Zaretskii
@ 2022-12-14 16:40   ` Robert Pluim
  2022-12-16  6:28     ` Jean Louis
  0 siblings, 1 reply; 5+ messages in thread
From: Robert Pluim @ 2022-12-14 16:40 UTC (permalink / raw)
  To: Eli Zaretskii; +Cc: 59531, Ramesh Nedunchezian

>>>>> On Thu, 24 Nov 2022 10:00:06 +0200, Eli Zaretskii <eliz@gnu.org> said:

    >> This return value is not very helpful.  The choice of a number ZERO
    >> as "Not A Number" doesn't help one to distinguish between the
    >> following two cases
    >> 
    >> (1) Input was a valid number, and it parses to number zero
    >> 
    >> (2) Input was NOT a valid number, and it was forcibly reported as ZERO
    >> 
    >> Consider amending `string-to-number` to throw an error (or return
    >> NIL) when the input is not parseable as a number, or providing an
    >> alternative API to validate numbers.  I am trying to parse some
    >> fields in an org table, and see if the field value is a number or
    >> not;

    Eli> Thanks.

    Eli> Changing the default behavior to signal an error is out of the question,
    Eli> since this is used in the Lisp reader and elsewhere, all over the place.  It
    Eli> is very useful there.

    Eli> However, as an enhancement, we could have an additional optional argument to
    Eli> request that the function signal an error if the string cannot be parsed as
    Eli> a number.

`cl-parse-integer' already has such an argument, but it only works for
integers. Alternatively, you could use `read-from-string' and check if
it returns something of type `integer' or `float'.

Robert
-- 





^ permalink raw reply	[flat|nested] 5+ messages in thread

* bug#59531: 29.0.50: An alternative to `string-to-number` which throws an error (or returns a NIL value) when input is non-parseable as number
  2022-12-14 16:40   ` Robert Pluim
@ 2022-12-16  6:28     ` Jean Louis
  0 siblings, 0 replies; 5+ messages in thread
From: Jean Louis @ 2022-12-16  6:28 UTC (permalink / raw)
  To: Robert Pluim; +Cc: Eli Zaretskii, 59531, Ramesh Nedunchezian

(defun string-is-number-p (s)
  "Return number only if string is actual number, otherwise NIL."
  (let* ((s (string-trim s)))
    (cond ((seq-empty-p s) nil)
	  ((string-match "[^0123456789\\.-]" s) nil)
	  ((string-match "-" s 1) nil)
	  ((numberp (string-to-number s)) (string-to-number s)))))


I use this function to make sure string is number and it
returns either the number of nil, and function is not
exhaustive.


Jean

Take action in Free Software Foundation campaigns:
https://www.fsf.org/campaigns

In support of Richard M. Stallman
https://stallmansupport.org/






^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2022-12-16  6:28 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-11-24  6:14 bug#59531: 29.0.50: An alternative to `string-to-number` which throws an error (or returns a NIL value) when input is non-parseable as number Ramesh Nedunchezian
2022-11-24  8:00 ` Eli Zaretskii
2022-12-14 16:40   ` Robert Pluim
2022-12-16  6:28     ` Jean Louis
2022-11-24 10:08 ` Jean Louis

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).