Alex Gordon
Alex Gordon

Reputation: 60811

C# proper way to loop through a float

I am looping through a function that looks like this:

Y=exp(-0.04*(x-13.25)^(2))*300

Let's say I want to get Yfor every value of x between 3.3454 and 20.3458 for every 0.1 interval

I would do this:

for (float i=3.3454;i<20.3458;i=+.1)

Is this the correct way to do this?

Upvotes: 1

Views: 7102

Answers (6)

Poosha
Poosha

Reputation: 1

using System.Math;
for (float i = MathF.Pi; i < 20.3458f; i = MathF.BitIncrement(i))

C# has a method MathF.BitIncrement() for floating point located in System.Math. Added in Dotnet core 3.0 in 2019.

There's also Math.BitIncrement() for double precision floating point and MathF.BitDecrement() for decrementing.

This is the algorithm:

public static float BitIncrement(float x)
        {
            int bits = BitConverter.SingleToInt32Bits(x);

            if ((bits & 0x7F800000) >= 0x7F800000)
            {
                // NaN returns NaN
                // -Infinity returns float.MinValue
                // +Infinity returns +Infinity
                return (bits == unchecked((int)(0xFF800000))) ? float.MinValue : x;
            }

            if (bits == unchecked((int)(0x80000000)))
            {
                // -0.0 returns float.Epsilon
                return float.Epsilon;
            }

            // Negative values need to be decremented
            // Positive values need to be incremented

            bits += ((bits < 0) ? -1 : +1);
            return BitConverter.Int32BitsToSingle(bits);
        }

Upvotes: 0

NerdFury
NerdFury

Reputation: 19214

Your syntax is slightly off, and some of the math functions you want to use will only work on doubles.

double y;
for(double x=3.3454; x<20.3458; x += .1) {
    y = Math.Exp(-0.04D * Math.Pow((x- 13.25D), 2D)) * 300;
    // do something with y
}

Upvotes: 5

eppdog
eppdog

Reputation: 423

I used a stack, but you could use anything to store the values:

        Stack YStack = new Stack();
        for(float x = 3.3454F; x < 20.3458F; x += .1F)
        {
            YStack.Push(Math.Exp(-0.04*Math.Pow(x-13.25,(2)))*300)
        }
        return YStack.ToArray();

Upvotes: 1

spender
spender

Reputation: 120498

If you add 0.1 each loop, you're going to end up with cumulative errors. It is not possible to precisely store 0.1 as a floating point number, and when you repeatedly add 0.1F/D, you'll start noticing drift. If you really want to get as close as possible, use integers for the loop then divide your integer values to calculate your floats.

Upvotes: 8

Conrad Frix
Conrad Frix

Reputation: 52675

You're missing the "F". Without which you'll get a compile error

literal of type double cannot be implicitly converted to type 'float'; use an 'F' suffix to create a literal of this type

for (float i = 3.3454F; i < 20.3458; i += .1F)

Also this is kinda dangerous, because the increment may not result in a change For example

        float f;
        f = float.MinValue;
        f += .1F;

        Console.WriteLine(f == float.MinValue);

Outputs True.

This is because a float is an approximation.

Upvotes: 2

kstev
kstev

Reputation: 740

You might want to change it to this

for (float i=3.3454;i<=20.3458;i+=0.1)

parenthesis need to be after the for keyword ;) also you had i=+.1 which is not how you would increase a variable and would create a syntax error

Upvotes: 1

Related Questions