[Asterisk-code-review] func_math: Return integer instead of float if possible (asterisk[master])

Kevin Harwell asteriskteam at digium.com
Thu Aug 19 11:13:40 CDT 2021


Kevin Harwell has submitted this change. ( https://gerrit.asterisk.org/c/asterisk/+/16079 )

Change subject: func_math: Return integer instead of float if possible
......................................................................

func_math: Return integer instead of float if possible

The MIN, MAX, and ABS functions all support float
arguments, but currently return floats even if the
arguments are all integers and the response is
a whole number, in which case the user is likely
expecting an integer. This casts the float to an integer
before printing into the response buffer if possible.

ASTERISK-29495

Change-Id: I902d29eacf3ecd0f8a6a5e433c97f0421d205488
---
M funcs/func_math.c
1 file changed, 15 insertions(+), 3 deletions(-)

Approvals:
  Joshua Colp: Looks good to me, but someone else must approve
  George Joseph: Looks good to me, approved
  Kevin Harwell: Approved for Submit



diff --git a/funcs/func_math.c b/funcs/func_math.c
index b8a6eb6..76d1a74 100644
--- a/funcs/func_math.c
+++ b/funcs/func_math.c
@@ -525,7 +525,11 @@
 	}
 
 	ast_debug(1, "%f is the minimum of [%f,%f]\n", response_num, num1, num2);
-	snprintf(buffer, buflen, "%f", response_num);
+	if ((int) response_num == response_num) {
+		snprintf(buffer, buflen, "%d", (int) response_num);
+	} else {
+		snprintf(buffer, buflen, "%f", response_num);
+	}
 
 	return 0;
 }
@@ -567,7 +571,11 @@
 	}
 
 	ast_debug(1, "%f is the maximum of [%f,%f]\n", response_num, num1, num2);
-	snprintf(buffer, buflen, "%f", response_num);
+	if ((int) response_num == response_num) {
+		snprintf(buffer, buflen, "%d", (int) response_num);
+	} else {
+		snprintf(buffer, buflen, "%f", response_num);
+	}
 
 	return 0;
 }
@@ -589,7 +597,11 @@
 
 	response_num = fabs(num1);
 	ast_debug(1, "%f is the absolute value of %f\n", response_num, num1);
-	snprintf(buffer, buflen, "%f", response_num);
+	if ((int) response_num == response_num) {
+		snprintf(buffer, buflen, "%d", (int) response_num);
+	} else {
+		snprintf(buffer, buflen, "%f", response_num);
+	}
 
 	return 0;
 }

-- 
To view, visit https://gerrit.asterisk.org/c/asterisk/+/16079
To unsubscribe, or for help writing mail filters, visit https://gerrit.asterisk.org/settings

Gerrit-Project: asterisk
Gerrit-Branch: master
Gerrit-Change-Id: I902d29eacf3ecd0f8a6a5e433c97f0421d205488
Gerrit-Change-Number: 16079
Gerrit-PatchSet: 2
Gerrit-Owner: N A <mail at interlinked.x10host.com>
Gerrit-Reviewer: Friendly Automation
Gerrit-Reviewer: George Joseph <gjoseph at digium.com>
Gerrit-Reviewer: Joshua Colp <jcolp at sangoma.com>
Gerrit-Reviewer: Kevin Harwell <kharwell at digium.com>
Gerrit-MessageType: merged
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.digium.com/pipermail/asterisk-code-review/attachments/20210819/8b3e1149/attachment.html>


More information about the asterisk-code-review mailing list