[ANN] CircuitComponentRounding.jl

I am pleased to announce CircuitComponentRounding.jl, a Julia package for rounding a set of e.g. calculated values to their nearest standardized values, which will actually exist in normal component storages! The following is taken from the README.md, which I think puts it nicely:

CircuitComponentRounding

From wikipedia:
“The E series is a system of preferred numbers (also called preferred values) derived for use in electronic components. It consists of the E3, E6, E12, E24, E48, E96 and E192 series, where the number after the ‘E’ designates the quantity of value “steps” in each series. Although it is theoretically possible to produce components of any value, in practice the need for inventory simplification has led the industry to settle on the E series for resistors, capacitors, inductors, and zener diodes.”

The use case

You use theory and math to calculate a set of components to be used in some circuit, e.g. a control system. But producers only manufacture components at certain values, which are unlikely to match your calculations. This creates two problems:

  1. Your calculated components are nowhere to be found among your real components. You then need to somehow figure out what is the best alternative.
  2. If you simulate the system with your calculated values, it will use different parameters than your physical system, because the calculated values are not physically available.

To remedy the situation, simply round your calculated values with the roundE function. The returned values can be directly plugged into your simulation, and the output can be formatted to match the labels in the component-storage.

But what series should I round to?

If you are not sure which series is available to you, use the function valuesE(series_number). This prints all the values in the given series series_number in the Julia REPL. Look for a series that matches the values seen in your component-storage.

Examples

Lets start with loading the package.

Julia> using CircuitComponentRounding

Use the valuesE function to see what values are in a given series.

julia> valuesE(3)
 100
 220
 470

Let’s now round some values! I will round to the E24 series, as it is the one available to me:

julia> vals = [3, 7e-7, 14e-2, 17e7]
4-element Vector{Float64}:
 3.0
 7.0e-7
 0.14
 1.7e8

# The second argument is the specific E series to round to
julia> roundE(vals, 24)
4-element Vector{Float64}:
 3.0
 6.8e-7
 0.15
 1.8e8

Adding the third positionional argument allows control of the output format:

julia> roundE(vals, 24, :ENG)
4-element Vector{String}:
 "3.0×10⁰"
 "680×10⁻⁹"
 "150×10⁻³"
 "180×10⁶"
julia> roundE(vals, 24, :SI)
4-element Vector{String}:
 "3.0"
 "680n"
 "150m"
 "180M"

As a final check, let’s check if all calculated values are in fact part of the E24 series, along with a value that is not. Note that factors of 10 are manually removed or added until the values range from 100 to 1000. This is because E-series repeat for every factor of 10, and is therefore only defined within one order of magnitude.

# Staring a line with `@.` makes everything occur elementwise
julia> @. [300, 680, 150, 151] in valuesE(24, print_to_repl=false)
4-element BitVector:
 1
 1
 1
 0

where 0 means false, and 1 means true. This final check is just for show, and should not be necessary during normal use.

Docstrings

This package aims to include elaborate docstrings for each function. If you forget how a function works, you can always check the docstring with ?function_name.

julia> ?
help?> valuesE

    valuesE(series_number)
    valuesE(series_number; print_to_repl=true, hundred_to_thousand=true, popfinal=true)

  Returns the values in the E-series series_number. This function is intended for printing the series values for
  comparison with available components. To get the values returned from the function as opposed to printed, set
  print_to_repl=false.  
  
  The other keyword arguments are related to technical details in the package internals, but are explained below for
  completeness:  
  
  If hundred_to_thousand=false, the values range from 1 to 10.  
  If popfinal=false, every series has 10 (or 1000) as its final value, even though this value is not part of the E-series.

  Examples
  ≡≡≡≡≡≡≡≡≡≡

  julia> valuesE(3)
  3-element Vector{Int64}:
   100
   220
   470

How the rounding is implemented

The rounding function returns the value with the smallest percentage error in the given E-series.
It does this by finding the geometric mean of the
two numbers in the given E-series ajecent to the given value (one smaller, one larger), and
returning the E-series value on the same side of the mean value as the input value.

In other words, if the input value is larger than the geometric mean, the returned value was rounded up. If the given input is smaller than the geometric mean, the output was rounded down. Rounding, in this case, means taking the first bigger/smaller value in the E-series.

Feedback

As this is the first package of a relatively novice programmer, feedback and input on ways the package could be better are very welcome!

3 Likes

Note that this package is depricated, and functionality has been moved to ESeriesRounding.jl. See the README.md there for updated examples.