From 52f0e6a29cb946da2fca88740012981de3814f51 Mon Sep 17 00:00:00 2001 From: bbbradsmith Date: Tue, 2 May 2023 23:57:32 -0400 Subject: [PATCH] Allow floating point constants to be converted to integer (warning if loss of precision) --- src/cc65/typeconv.c | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/cc65/typeconv.c b/src/cc65/typeconv.c index f77ec3951..004072c03 100644 --- a/src/cc65/typeconv.c +++ b/src/cc65/typeconv.c @@ -123,6 +123,16 @@ static void DoConversion (ExprDesc* Expr, const Type* NewType) ** to handle sign extension correctly. */ + /* If this is a floating point constant, convert to integer, + ** and warn if precision is discarded. + */ + if (IsClassFloat (OldType) && IsClassInt (NewType)) { + long IVal = (long)Expr->V.FVal.V; + if (Expr->V.FVal.V != FP_D_FromInt(IVal).V) + Warning ("Floating point constant (%f) converted to integer loses precision (%d)",Expr->V.FVal.V,IVal); + Expr->IVal = IVal; + } + /* Check if the new datatype will have a smaller range. If it ** has a larger range, things are OK, since the value is ** internally already represented by a long.