Okay basically I have a class that manages sound and it's volume is a floating point value with 1.00f being 100% and 0.00f being 0%. So the math is done in low values. Allowing more precise calculations. Anyways, I'm adding and subtracting from the value by 0.05f as per the user's input. Well my problem is when it's at 1.00f and I subtract 0.05f from it it becomes 0.94f instead of 0.95f like it should. For the visual output I'm displaying an integer representation multiplied by 100 of the value.

if (Input.IsLeft())
            {
                if (MusicVolume > 0f && SelectedIndex == 0)
                    MusicVolume -= 0.05f;

                if (MusicVolume < 0)
                    MusicVolume = 0;
            }

            if (Input.IsRight())
            {
                if (MusicVolume < 1f && SelectedIndex == 0)
                    MusicVolume += 0.05f;

                if (MusicVolume > 1)
                    MusicVolume = 1;
            }

            MusicVolumeString = ((int)(MusicVolume * 100).ToString() + " %";

When subtracting from 1.00f it goes to 0.94f instead of 0.95f and when coming up from 0 it goes to 0.05 instead 0.06 like I would expect it to since it's dropping from 1.00 to 0.94. Any help on this is appreciated.

Thanks,
Jamie

Recommended Answers

All 7 Replies

Are you sure that the actual MusicVolume float is actually wrong, or is it the string that is converting it wrong?

Well the MediaPlayer and SoundEffectInstance classes from XNA use floating points as the value for their volume, and considering that's what my game is using in the audio manager I'm wanting to stick with floating points. I can try multiplying it by 100.01 but I don't think that will work either.

I take that last post back, it did work haha. I just multiplied by 100.01f instead of 100 in the string conversion and it's giving the correct values now.

I take that last post back, it did work haha. I just multiplied by 100.01f instead of 100 in the string conversion and it's giving the correct values now.

Try just multiplying it by '100f'.

Well as I stated, multiplying by 100.01f gave the correct output values. But multiplying a float by 100 I believe is automatically assumed by the compiler to be 100f. I only add the 'f' when dealing with double style values because otherwise it gives an error. So I just tried 100.01f simply to see if it would work and it did. I'll give 100f a try and post back later if it works or not so that others will know.

Well as I stated, multiplying by 100.01f gave the correct output values. But multiplying a float by 100 I believe is automatically assumed by the compiler to be 100f. I only add the 'f' when dealing with double style values because otherwise it gives an error. So I just tried 100.01f simply to see if it would work and it did. I'll give 100f a try and post back later if it works or not so that others will know.

I don't think that's correct. I think the compiler is interpretting 100 as an integer, whereas if you add a .01 or an f to the end it gets interpretted as a floating point number.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.