Main Content

Invalid scientific notation format

the use of an invalid format makes the code less readable

Since R2022b

Description

This issue occurs when you express a floating-point literal in exponential form but you do not use the standard scientific notation format of one nonzero digit before the decimal.

Polyspace® does not flag the use of zero before the decimal to represent literal 0.0 in exponential form, for instance 0.00E+0.

Risk

The use of non-standard scientific notations to represent literals makes your code less readable and more error-prone.

Fix

Use the standard format of one nonzero digit before the decimal to express floating-point literals in scientific notation.

Examples

expand all

#include <stdio.h>

void printUsingEFormat()
{
	float literalVals[] = {
		1154.12e+5L, //more than one digit before decimal
		.1E4F,       //no digit before decimal
		58.L,
		0.1E4F,     //zero digit before decimal
		1.34e+4,
		13.4e+3,    //more than one digit before decimal
		0.0000E+0,
		0xa.bp10l,
		0x15.0p5};
	for (int i = 0;
		 i < sizeof(literalVals) / sizeof(literalVals[0]);
		 ++i)
	{
		printf("%.6e\n", literalVals[i]);
	}
}

In this example, function printUsingEFormat() cycles through the elements of array literalVals and prints each element in scientific notation format.

Polyspace flags every literal that does not use a standard scientific notation format. The other array elements use the correct format or are not represented using scientific notation.

Result Information

Group: Good Practice
Language: C | C++
Default: Off
Command-Line Syntax: INVALID_NOTATION_ON_E_CONSTANT
Impact: Low

Version History

Introduced in R2022b