# Tag Archives: calculations

## Control a loop that uses floating-point values in C#

Sometimes you might need to use a floating-point variable that loop over a range of values. For example, you might want a value to step through the values 0.0 through 1.0 in increments of 0.1. The sequence of numbers would … Continue reading

Posted in calculations, mathematics, variables | | Comments Off on Control a loop that uses floating-point values in C#

## Compare floating-point values safely in C#

The computer stores values, including floating-point values, in binary using 0s and 1s. That means it cannot store all possible decimal values exactly. Sometimes when you multiply or divide two floating-point values, the computer is unable to store the result … Continue reading

## Calculate credit payments in WPF and C#

This is a WPF version of the example Calculate credit payments in C#. The calculations are the same. Unfortunately building and using the user interface is much harder in WPF, particularly working with the ListView control. (The unofficial WPF slogan: … Continue reading

Posted in algorithms, calculations, finance, mathematics | | Comments Off on Calculate credit payments in WPF and C#

## Use the BigInteger structure in C#

The long data type can hold values between roughly -9.2 x 1018 and -9.2 x 1018, but sometimes even that range isn’t big enough. The .NET Framework 4.0 (C# 4.0 or Visual Studio 2010) introduced the BigInteger structure to represent … Continue reading

Posted in mathematics | | 2 Comments

## Use “banker’s rounding” and “normal rounding” in C#

By default the Math.Round method uses “banker’s rounding.” In banker’s rounding a number with a final digit of 5 is rounded to the nearest even number rather than to the next larger number as you might expect. The idea is … Continue reading

Posted in algorithms, mathematics | | Comments Off on Use “banker’s rounding” and “normal rounding” in C#

## Define and use bit masks in C#

The example Understand bit masks in C# explains how to use bit masks. To define a bit mask, simply create an enum and give it the Flags attribute as in the following code. [Flags] private enum BitmaskEnum { Value1 = … Continue reading

Posted in algorithms, calculations, mathematics, variables | | Comments Off on Define and use bit masks in C#

## Understand bit masks in C#

Some values, including some properties defined by the .NET Framework, are bit masks. That means each bit in a value means something. For example, the AnchorStyles enumeration that determines how controls are anchored in their parents defines four values: Top, … Continue reading

Posted in algorithms, calculations, mathematics, variables | | Comments Off on Understand bit masks in C#

## Use bit operations in C#

C# defines several operators that perform bit operations. As you may be able to guess from the names, these operators manipulate the bits in an integer value. They operate on the value’s bits separately so they are sometimes called “bitwise … Continue reading

Posted in calculations, mathematics, variables | | Comments Off on Use bit operations in C#

## Compare speeds of arithmetic operations in C#

This example compares the speeds of arithmetic operations with different data types. There’s a big difference between the speeds of operations using the decimal data type and the other types, so performing a loop a certain number of time is … Continue reading

Posted in algorithms, calculations, mathematics | | Comments Off on Compare speeds of arithmetic operations in C#

## Calculate credit payments in C#

For each month the program: Calculates the payment first. This is either a percent of the balance or the minimum amount, whichever is greater. Calculates interest after the payment is calculated but before it is subtracted from the balance. Adds … Continue reading

Posted in algorithms, calculations, finance, mathematics | | 11 Comments