开发者

Why nomalise a float &... why is alpha a 0 to 1 float point number in CSS rgba()

to-the-point question here. sorry.

I am trying to understand *normalization** in terms of mathematics.

In the toxiclibs library one can normalise a float. Why one would want to normalize a float... In 3D I get that a normal of 3 dimentional plane can give the up. First thought: it is to do with preformance. But then I found this SO question explaining how a game UI may use it. This... I kind of get.

Would it be fair to say one would normalised a float where there is a max and minimum? Two scenarios I can think of:

  1. CSS Color RGBA: RGB is from 0 to 255 but could, theoretically, go higher. Whereas A is from 0.0 to 1.0. Is this normalized as it can be com开发者_高级运维pletely transparent or opaque, therefore it is a normal because there is a definative 0 or 1?

  2. When recording music (or looking at a wave form), it can be clipped. Is this because there is a max and a min, so a normalized value above 1 would be clipped? I guess, in this case, normalise would be for convenience.

So, why normalize a float? Is it performance? Readabilty? Something else (visual reference == Kudos)? Does a Spinal Tap Amp going all the way up to 11 have anything to do with this?


A brief explanation of "normalized" floats

In scientific notation, you write any number as x.y * 10^z, where x is a single non-zero digit. For example 212 = 2.12 * 10^2. It's always possible for x to be a single digit, because you can keep dividing by 10. Likewise, it's always possible for x to be non-zero, because you can keep multiplying by 10 except when trying to write the value 0. 0 in scientific notation just ends up as 0.0 * 10^0.

More about scientific notation

Moving on to floats... Floats are basically scientific notation in binary. They are written in the form x.y * 2^z. x is still a single non-zero digit, but in binary that only leaves one option: 1! If you're implementing floating point storage in a computer, you don't want to waste a bit that is always 1, so you only store y and z (and +/-).

But now how do you store 0? It turns out that a special value of z is used to mean "x is actually 0." Then you can store 0.0. But you can also store 0.0010100011000 * 2^(special z) and all sorts of "denormal" values.

I don't understand what toxiclib's normalize function does - I was unable to find the documentation. As far as I was aware, denormal floats have no equivalent normalized representation for the same precision float (a denormal single could be represented as a normal double, but not as a normal single). Perhaps that's what the function is doing. But unless you're dealing with some really low-level stuff or high-precision, you probably don't care.

Eric Lippert has a good explanation of the anatomy of a float.


I know this is a stale thread, but FWIW (I'm the author of the above mentioned toxiclibs) - I usually prefer to work with normalized values (i.e. both in the 0.0 .. 1.0 or -1.0 ... +1.0 interval) simply because it keeps them more flexible when working in multiple domains. You simply have an associated scale value or mapping coefficient which is applied whenever the value is used. This allows me to keep the actual absolute range definitions outside the code and/or use a more human friendly number range for GUI elements. So I tend to think of them as percentages and it's a little bit like MVC on a nano-scale.

E.g. I might have a number of config files defining different height ranges for a 3D character. My tool doesn't care what the absolute values are, but it might have some special cases for say anything >80% (0.8). My model is only using normalized values.

Config A might be: min:50 - max:200
Config B: min:20 - max: 100

If my height value is internally just expressed between 0.0 .. 1.0, then I can much better work with that and computing the real absolute value (i.e. the "view" of that model parameter) is dead easy:

float val = config.min + (config.max-config.min) * normVal;

Now if there're other parameters (with their own ranges) dependent on my height param, the normalized state makes it easy to apply it to their domains (and avoid unnecessary re-mapping)...


why one would want to normalize a float

To make as many bits as possible meaningful.

If the float is always between 0.5 and 1.0, the left-most bit can be assumed to be 1. One extra bit of precision.


There is no one concept of normalizing. The normal of a plane is different from normalizing a vector, which is different from normalizing a float.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜