[OpenSIPS-Devel] [opensips] Make s.int also operate on floats/doubles by truncating them to an int (#836)

Liviu Chircu notifications at github.com
Tue Mar 22 20:14:20 CET 2016


> +				if(val->rs.len == 0) {
> +					val->ri = 0;
> +				//Try converting to INT
> +				} else if(str2sint(&val->rs, &val->ri)!=0) {
> +					//Try converting to Double
> +					errno = 0;
> +					d = strtod(val->rs.s, NULL);
> +
> +					if(errno != 0) {
> +						return -1;
> +					}
> +
> +					//Use truncation for getting int value. Ensures consistent treatment
> +					//trunc(1.5) is 1.0 and trunc(-1.5) is -1.0
> +					//mathops gives users more rounding options if they require them
> +					val->ri = trunc(d);

The only benefit to adding additional error handling by the use of `strtod` and `trunc` is to parse the following values, which `str2sint` is not able to:

* signed numbers, prefixed by `+` or `-`
* `INF` or `INFINITY`
* `NAN` (not a number)

Apart from the above negligible benefits, `str2sint` will successfully parse and truncate all input that's fed to it. Coupled with the fact that we now have to link against `lm`, I think we should rather:

* update the function comments on `str2sint`, so that the truncation / partial parse (just like `strtod` and friends) is obvious to all devs
* avoid further error handling for `str2sint` and just use its result, after it parsed all it could from the given input

---
You are receiving this because you are subscribed to this thread.
Reply to this email directly or view it on GitHub:
https://github.com/OpenSIPS/opensips/pull/836/files/be25fc66e51c7e8358f5156c7d6d1054ef483c5d#r57050628
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.opensips.org/pipermail/devel/attachments/20160322/35a2f03c/attachment-0001.htm>


More information about the Devel mailing list