Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: Added support for generating logarithmic units #45

Merged

Conversation

bretthysuik
Copy link
Contributor

  • "Logarithmic" and "LogarithmicScalingFactor" objects are used in the json file to indicate the unit is logarithmic. These objects are optional and assumed to be false if not specified.
  • Added a separate PowerShell script to generate the logarithmic arithmetic operator source code and logarithmic arithmetic operator unit test source code.
  • This lays the groundwork for being able to auto-generate logarithmic units like any other unit. For example, it should be possible to auto-generate a decibel unit as well as any sub-unit specified here using this code. I've tested it on voltage ratio--as seen below with units of dBV, dBmV, dBuV-- and power ratio and it works nicely.
  • I'm adding this feature so that I can next implement Level (with units of decibels), ElectricPotentialRatio, and PowerRatio. I'm not sure if you want these units in your library however, since they're specialty units related to RF, electronics, and audio engineering (this is the area I work in which is why I'd like to add them).

Example:

{
    "Name": "ElectricPotentialRatio",
    "BaseUnit": "DecibelVolt",
    "Logarithmic" : "True",
    "LogarithmicScalingFactor" : "2",
    "XmlDoc": "The strength of a signal expressed in decibels (dB) relative to one volt RMS.",
    "Units": [
        {
            "SingularName": "DecibelVolt",
            "PluralName": "DecibelVolts",
            "FromUnitToBaseFunc": "x",
            "FromBaseToUnitFunc": "x",
            "Localization": [
                {
                    "Culture": "en-US",
                    "Abbreviations": ["dBV"]
                }
            ]
        },
        {
            "SingularName": "DecibelMicrovolt",
            "PluralName": "DecibelMicrovolts",
            "FromUnitToBaseFunc": "x - 120",
            "FromBaseToUnitFunc": "x + 120",
            "Localization": [
                {
                    "Culture": "en-US",
                    "Abbreviations": ["dBµV"]
                }
            ]
        },
        {
            "SingularName": "DecibelMillivolt",
            "PluralName": "DecibelMillivolts",
            "FromUnitToBaseFunc": "x - 60",
            "FromBaseToUnitFunc": "x + 60",
            "Localization": [
                {
                    "Culture": "en-US",
                    "Abbreviations": ["dBmV"]
                }
            ]
        }
    ]
}

- "Logarithmic" and "LogarithmicScalingFactor" are used in the json file to indicate the unit is logarithmic.
- Added a separate PowerShell script to generate the logarithmic arithmetic operator source code.
@angularsen
Copy link
Owner

Awesome work. I actually looked at decibel and non-linear units myself, but didn't take it anywhere. If you can look at my comments then I'd be happy to merge this in.

As for the domain specific units, I'm not sure whether they should be part of the main library or not. I can easily see a domain specific nuget with dependency on UnitsNet, such as UnitsNet.Audio. What do you think and if you agree, what naming would be natural? Setting up a new nuget would involve creating a new set of projects for Audio, setting the output path to a Audio subfolder of the current output path and modifying the build scripts to build, pack and publish both nugets.

On the other hand, it's still a small library and maybe we can postpone splitting up into multiple nugets until there is a demand for it and possibly many more domain specific units, and bump the major version to indicate breaking changes. At this point I'm happy with either way.

- Added documentation to logarithmic source code generator functions.
- Tweaked various comments and names.
- Inverted logic when checking if a unit is logarithmic so it's easier to read/understand.
@bretthysuik
Copy link
Contributor Author

On the other hand, it's still a small library and maybe we can postpone splitting up into multiple nugets until there is a demand for it and possibly many more domain specific units, and bump the major version to indicate breaking changes. At this point I'm happy with either way.

I'm going to open another discussion topic for this since I have a few ideas and I don't want to clutter up this thread.

@bretthysuik
Copy link
Contributor Author

Don't merge this, I found a bug in the addition/subtraction unit test code.

EDIT: Fixed.

…tests

- The previous calculation is incorrect in certain situations. There's no way to define the expected value without re-implementing the addition/subtraction algorithm.
- Developers are now responsible for creating custom logarithmic addition and subtraction unit tests.
@angularsen
Copy link
Owner

You'll have to regenerate the files after that last commit. Other than that, is this ready to merge?

@bretthysuik
Copy link
Contributor Author

You'll have to regenerate the files after that last commit.

There shouldn't be anything to regenerate since none of the existing units are logarithmic and the new scripts don't modify existing non-logarithmic units.

Other than that, is this ready to merge?

Yes

angularsen added a commit that referenced this pull request Nov 7, 2014
Feature: Added support for generating logarithmic units
@angularsen angularsen merged commit 8f149d3 into angularsen:develop Nov 7, 2014
@angularsen
Copy link
Owner

My bad, I forgot this was just preparation for adding the actual logarithmic units later. Great work!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants