Sneaky Electrons

Miscellaneous Technical Stuff.

EIA Standard Resistor Value Approximation in JavaScript

While working on my bi-directional resistor color code calculator, one of the first things I stumbled upon was how to normalize the user supplied resistance value.
Generally, resistors come in predefined values, or “preferred values”. These values were defined by Electronic Industries Association (EIA) (IEC 60038) and consist of about 6 different series with each series corresponding to particular tolerances:

E6    20% tolerance
E12   10% tolerance
E24   5% tolerance
E48   2% tolerance
E96   1% tolerance
E192  0.5, 0.25, 0.1% tolerances

Full table

Such division ensures that substituting an arbitrary value with nearest preferred value wouldn’t exceed the required tolerance. In other words, given a tolerance and an arbitrary resistance value we can calculate the preferred value using the above table.

Fortunately there is much neater way to do this. It is possible to approximate E-series preferred values (which is basically Renard series with different interval steeping) with the following equation (author unknown):

Where R is the resistance, round() is round to nearest integer function, and round2decimal() is round to 2 decimal places function.
The equation seemed to work for E48 and E96 series, but gave wrong values for other. The code below is its adaptation for JavaScript and for the rest of the series E12/24/192. The function accepts resistance value in Ohm plus tolerance and produces closest preferred value.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
   function log10(val) {
      return Math.log(val) / Math.LN10;
  }

        function EIAValues(val, tolerance) {
            var series;

            if (tolerance == 20) series = 6;
            else if (tolerance == 10) series = 12;
            else if (tolerance == 5) series = 24;
            else if (tolerance == 2) series = 48;
            else if (tolerance == 1) series = 96;
            else series = 192;

            var l = log10(val);

            var decplaces = series < 48 ? 10 : 100;

            var pref_val = (Math.round((Math.pow(10, (Math.round(series * l) / series)) / Math.pow(10, Math.floor(log10((Math.pow(10, (Math.round(series * l) / series))))))) * decplaces) / decplaces) * Math.pow(10, Math.floor(log10((Math.pow(10, (Math.round(series * l) / series))))));

            // compensate for possible precision loss in the above calculation
            var rounded = Math.round(pref_val);
            var abs = Math.abs(rounded - pref_val);
            if (abs > 0.999 || abs < 0.0001)
                pref_val = rounded;

            if (pref_val >= 260 && pref_val <= 460) pref_val += 10; // fix for E24/E12/E6 series   
            else if (pref_val == 830) pref_val -= 10;               // fix for E24/E12/E6 series
            else if (pref_val == 919) pref_val++;                   // fix for E192 series

            return pref_val;
        }

Comments