JavaScript is not currently enabled, but is required for full CodeSonar manual search and browse functionality.
If you are viewing this file in your hub's Web GUI, enable JavaScript in your browser: you will also need it for GUI functionality.
If you opened this file directly from disk, your browser may be directly suppressing JavaScript functionality: certain browsers perform this suppression on local files (but not files delivered by web servers) for security reasons.
| CodeSonar® 9.2p0 | CONFIDENTIAL | CodeSecure Inc |
The result of an integral computation that might lose precision is cast into a floating point value.
Floating point operations are by nature approximated. This can introduce bugs when precise, mathematical properties are expected from inherently imprecise floating point computations. Moreover, computations that might lose precision or overflow, whose result is stored in a larger type, are suspicious since, by computing on the larger type from the beginning, one could avoid approximations and overflows.
| Class Name | Cast: Integer to Floating Point (C#) | |||
|---|---|---|---|---|
| Significance | reliability | |||
| Mnemonic | CSHARP.CAST.FTRUNC | |||
| Categories |
|
|||
| Availability | Available for C# only. |
|||
| Enabling | Checks for this warning class are enabled by
default. To disable them, add the following WARNING_FILTER rule to the
project configuration file.
WARNING_FILTER += discard class="Cast: Integer to Floating Point (C#)" |
using System;
namespace IntApproxExamples
{
public class DocumentationExamples
{
public static void Main(string[] args)
{
double d = (double) (Exp1() / Exp2()); // "Cast: Integer to Floating Point (C#)" warning issued here
// - d is assigned 0; programmer probably intended to assign 0.5
Console.WriteLine("d = " + d);
}
private static int Exp1()
{
return 3+1;
}
private static int Exp2()
{
return 6+2;
}
}
}
The programmer probably intended to perform floating point division, in which case it is sufficient to adjust the parentheses on the assignment expression. For example, it could be rewritten as
If the result of a computation requires a particular type, compute from the beginning on that type instead of applying a type conversion at the end of the computation. Make sure you place the brackets correctly when performing math operations involving a cast.
The following configuration file parameters affect checks for this warning class.
To report problems with this documentation, please visit https://support.codesecure.com/.