Main Content

CERT C: Rec. FLP07-C

R2026b

Cast the return value of a function that returns a floating-point type

Since R2026b

Description

Cast the return value of a function that returns a floating-point type.1

Polyspace Implementation

Polyspace® checks for the issue Imprecise returned float.

Examples

expand all

Issue

The issue occurs when the return type of a function is float but:

  • The return expression is a hard-coded literal without an explicit cast to the return type.

  • The return expression is an arithmetic expression involving literals without an explicit cast of the entire expression to the return type.

The checker does not report violations on return expressions that do not contain any literals.

Risk

The returned value can have different precision than the declared return type on different platforms, leading to inconsistent program execution across architectures. Comparisons or subsequent arithmetic with the returned value can produce unexpected results because of differences in precision.

Fix

Cast the return expression to the declared return type of the function.

Example — Floating-Point Return Value Without Explicit Cast

In this example, the function calc_percentage returns a floating-point expression without an explicit cast. Polyspace reports a violation on the return statement.


float calc_percentage(float value) {
    return value * 0.1f;    // Noncompliant
}

void use_percentage(float x) {
    double y = calc_percentage(x);
}

The expression value * 0.1f involves a hard-coded literal and can retain higher precision than float on some platforms. Without an explicit cast in the return statement, the caller receives a value with unpredictable precision.

Correction — Cast Return Expression to Declared Type

The corrected code casts the return expression to float, removing any excess precision.


float calc_percentage(float value) {
    return (float)(value * 0.1f); // Compliant
}

void use_percentage(float x) {
    double y = calc_percentage(x);
}

Check Information

Group: Rec. 05. Floating Point (FLP)
PQL Name: std.cert.FLP07_C

Version History

Introduced in R2026b


1 This software has been created by MathWorks incorporating portions of: the “SEI CERT-C Website,” © 2017 Carnegie Mellon University, the SEI CERT-C++ Web site © 2017 Carnegie Mellon University, ”SEI CERT C Coding Standard – Rules for Developing safe, Reliable and Secure systems – 2016 Edition,” © 2016 Carnegie Mellon University, and “SEI CERT C++ Coding Standard – Rules for Developing safe, Reliable and Secure systems in C++ – 2016 Edition” © 2016 Carnegie Mellon University, with special permission from its Software Engineering Institute.

ANY MATERIAL OF CARNEGIE MELLON UNIVERSITY AND/OR ITS SOFTWARE ENGINEERING INSTITUTE CONTAINED HEREIN IS FURNISHED ON AN "AS-IS" BASIS. CARNEGIE MELLON UNIVERSITY MAKES NO WARRANTIES OF ANY KIND, EITHER EXPRESSED OR IMPLIED, AS TO ANY MATTER INCLUDING, BUT NOT LIMITED TO, WARRANTY OF FITNESS FOR PURPOSE OR MERCHANTABILITY, EXCLUSIVITY, OR RESULTS OBTAINED FROM USE OF THE MATERIAL. CARNEGIE MELLON UNIVERSITY DOES NOT MAKE ANY WARRANTY OF ANY KIND WITH RESPECT TO FREEDOM FROM PATENT, TRADEMARK, OR COPYRIGHT INFRINGEMENT.

This software and associated documentation has not been reviewed nor is it endorsed by Carnegie Mellon University or its Software Engineering Institute.