Reputation: 3549
I have property for the life of an particle (I'm simulating particle systems) which is a float value, because I also use it for transparency (alpha is float). I have read questions about converting float to double and realized that it is quite a problem, so I'll probably can't convert float to double. Problem is that I want try to calculate path of the particle from the life variable like:
particle.x += particle.xi;
particle.xi = Math.Sin(life);
Note: life value is at the beginning 1.0f and decreasing to 0 (if its <0.0f I reinitialize the particle and set the life to 1.0f).
But the Sin wants the the double value ... and we are back at the beginning.
So one solution can be changing the type of life property to double and when I use it for transparency I will just this double convert to float which shouldn't be big problem (I guess).
But my question is if is there any other way to do it, cause double also cost more memory (I don't know what more means in this case - I guess to times more, lets say I'll have 500 particles and each will have this property). I just need somehow calculate sin value from this float property.
So is it possible? Are my concerns about memory important?
Upvotes: 0
Views: 6458
Reputation: 57932
This should work:
particle.x += particle.xi;
particle.xi = (float) Math.Sin(life);
It doesn't "use more memory", it simply converts the value to a double temporarily while it's recalculating it, then converts it back to a float when it goes to store the value.
To go into a bit more detail: that Math.Sin requires a double, but the float can be converted to this higher precision without loss, so it just "magically works" (an implicit cast from float to double). However, to convert the resulting double back to a float, you will be reducing the precision of the number and so the compiler (which doesn't know if this will be acceptable to you) won't do it unless you force it to (by using (float)
which is an explicit cast from double to float).
Upvotes: 3