├── .github ├── FxPT.png └── ISSUE_TEMPLATE │ ├── bug_report.md │ └── feature_request.md ├── .gitignore ├── LICENSE ├── README.md └── Types ├── Tests ├── FixedNumberTests.cs └── FixedTypes.Tests.csproj ├── Types.sln └── Types ├── Fixed32.cs └── FixedTypes.csproj /.github/FxPT.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/stormmuller/fixed-point-types/5ff45cad4022081a58de7a8097f16439ef897634/.github/FxPT.png -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/bug_report.md: -------------------------------------------------------------------------------- 1 | --- 2 | name: Bug report 3 | about: Create a report to help us improve 4 | title: '' 5 | labels: '' 6 | assignees: '' 7 | 8 | --- 9 | 10 | **Describe the bug** 11 | A clear and concise description of what the bug is. 12 | 13 | **To Reproduce** 14 | Steps to reproduce the behavior: 15 | 1. Code Snippet to frame the issue 16 | 2. Dotnet version you're using(if issue is not related to library itself) 17 | 18 | **Expected behavior** 19 | Show a clear and concise description of what you expected to happen. As well as what actually happened. 20 | 21 | **Additional context** 22 | Add any other context about the problem here. 23 | -------------------------------------------------------------------------------- /.github/ISSUE_TEMPLATE/feature_request.md: -------------------------------------------------------------------------------- 1 | --- 2 | name: Feature request 3 | about: Suggest an idea for this project 4 | title: '' 5 | labels: '' 6 | assignees: '' 7 | 8 | --- 9 | 10 | **Is your feature request related to a problem? Please describe.** 11 | A clear and concise description of what the problem is. Ex. I'm always frustrated when [...] 12 | 13 | **Describe the solution you'd like** 14 | A clear and concise description of what you want to happen. 15 | 16 | **Describe alternatives you've considered** 17 | A clear and concise description of any alternative solutions or features you've considered. 18 | 19 | **Additional context** 20 | Add any other context or screenshots about the feature request here. 21 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | ## Ignore Visual Studio temporary files, build results, and 2 | ## files generated by popular Visual Studio add-ons. 3 | ## 4 | ## Get latest from https://github.com/github/gitignore/blob/master/VisualStudio.gitignore 5 | 6 | # User-specific files 7 | *.suo 8 | *.user 9 | *.userosscache 10 | *.sln.docstates 11 | 12 | # User-specific files (MonoDevelop/Xamarin Studio) 13 | *.userprefs 14 | 15 | # Build results 16 | [Dd]ebug/ 17 | [Dd]ebugPublic/ 18 | [Rr]elease/ 19 | [Rr]eleases/ 20 | x64/ 21 | x86/ 22 | bld/ 23 | [Bb]in/ 24 | [Oo]bj/ 25 | [Ll]og/ 26 | 27 | # Visual Studio 2015/2017 cache/options directory 28 | .vs/ 29 | # Uncomment if you have tasks that create the project's static files in wwwroot 30 | #wwwroot/ 31 | 32 | # Visual Studio 2017 auto generated files 33 | Generated\ Files/ 34 | 35 | # MSTest test Results 36 | [Tt]est[Rr]esult*/ 37 | [Bb]uild[Ll]og.* 38 | 39 | # NUNIT 40 | *.VisualState.xml 41 | TestResult.xml 42 | 43 | # Build Results of an ATL Project 44 | [Dd]ebugPS/ 45 | [Rr]eleasePS/ 46 | dlldata.c 47 | 48 | # Benchmark Results 49 | BenchmarkDotNet.Artifacts/ 50 | 51 | # .NET Core 52 | project.lock.json 53 | project.fragment.lock.json 54 | artifacts/ 55 | **/Properties/launchSettings.json 56 | 57 | # StyleCop 58 | StyleCopReport.xml 59 | 60 | # Files built by Visual Studio 61 | *_i.c 62 | *_p.c 63 | *_i.h 64 | *.ilk 65 | *.meta 66 | *.obj 67 | *.iobj 68 | *.pch 69 | *.pdb 70 | *.ipdb 71 | *.pgc 72 | *.pgd 73 | *.rsp 74 | *.sbr 75 | *.tlb 76 | *.tli 77 | *.tlh 78 | *.tmp 79 | *.tmp_proj 80 | *.log 81 | *.vspscc 82 | *.vssscc 83 | .builds 84 | *.pidb 85 | *.svclog 86 | *.scc 87 | 88 | # Chutzpah Test files 89 | _Chutzpah* 90 | 91 | # Visual C++ cache files 92 | ipch/ 93 | *.aps 94 | *.ncb 95 | *.opendb 96 | *.opensdf 97 | *.sdf 98 | *.cachefile 99 | *.VC.db 100 | *.VC.VC.opendb 101 | 102 | # Visual Studio profiler 103 | *.psess 104 | *.vsp 105 | *.vspx 106 | *.sap 107 | 108 | # Visual Studio Trace Files 109 | *.e2e 110 | 111 | # TFS 2012 Local Workspace 112 | $tf/ 113 | 114 | # Guidance Automation Toolkit 115 | *.gpState 116 | 117 | # ReSharper is a .NET coding add-in 118 | _ReSharper*/ 119 | *.[Rr]e[Ss]harper 120 | *.DotSettings.user 121 | 122 | # JustCode is a .NET coding add-in 123 | .JustCode 124 | 125 | # TeamCity is a build add-in 126 | _TeamCity* 127 | 128 | # DotCover is a Code Coverage Tool 129 | *.dotCover 130 | 131 | # AxoCover is a Code Coverage Tool 132 | .axoCover/* 133 | !.axoCover/settings.json 134 | 135 | # Visual Studio code coverage results 136 | *.coverage 137 | *.coveragexml 138 | 139 | # NCrunch 140 | _NCrunch_* 141 | .*crunch*.local.xml 142 | nCrunchTemp_* 143 | 144 | # MightyMoose 145 | *.mm.* 146 | AutoTest.Net/ 147 | 148 | # Web workbench (sass) 149 | .sass-cache/ 150 | 151 | # Installshield output folder 152 | [Ee]xpress/ 153 | 154 | # DocProject is a documentation generator add-in 155 | DocProject/buildhelp/ 156 | DocProject/Help/*.HxT 157 | DocProject/Help/*.HxC 158 | DocProject/Help/*.hhc 159 | DocProject/Help/*.hhk 160 | DocProject/Help/*.hhp 161 | DocProject/Help/Html2 162 | DocProject/Help/html 163 | 164 | # Click-Once directory 165 | publish/ 166 | 167 | # Publish Web Output 168 | *.[Pp]ublish.xml 169 | *.azurePubxml 170 | # Note: Comment the next line if you want to checkin your web deploy settings, 171 | # but database connection strings (with potential passwords) will be unencrypted 172 | *.pubxml 173 | *.publishproj 174 | 175 | # Microsoft Azure Web App publish settings. Comment the next line if you want to 176 | # checkin your Azure Web App publish settings, but sensitive information contained 177 | # in these scripts will be unencrypted 178 | PublishScripts/ 179 | 180 | # NuGet Packages 181 | *.nupkg 182 | # The packages folder can be ignored because of Package Restore 183 | **/[Pp]ackages/* 184 | # except build/, which is used as an MSBuild target. 185 | !**/[Pp]ackages/build/ 186 | # Uncomment if necessary however generally it will be regenerated when needed 187 | #!**/[Pp]ackages/repositories.config 188 | # NuGet v3's project.json files produces more ignorable files 189 | *.nuget.props 190 | *.nuget.targets 191 | 192 | # Microsoft Azure Build Output 193 | csx/ 194 | *.build.csdef 195 | 196 | # Microsoft Azure Emulator 197 | ecf/ 198 | rcf/ 199 | 200 | # Windows Store app package directories and files 201 | AppPackages/ 202 | BundleArtifacts/ 203 | Package.StoreAssociation.xml 204 | _pkginfo.txt 205 | *.appx 206 | 207 | # Visual Studio cache files 208 | # files ending in .cache can be ignored 209 | *.[Cc]ache 210 | # but keep track of directories ending in .cache 211 | !*.[Cc]ache/ 212 | 213 | # Others 214 | ClientBin/ 215 | ~$* 216 | *~ 217 | *.dbmdl 218 | *.dbproj.schemaview 219 | *.jfm 220 | *.pfx 221 | *.publishsettings 222 | orleans.codegen.cs 223 | 224 | # Including strong name files can present a security risk 225 | # (https://github.com/github/gitignore/pull/2483#issue-259490424) 226 | #*.snk 227 | 228 | # Since there are multiple workflows, uncomment next line to ignore bower_components 229 | # (https://github.com/github/gitignore/pull/1529#issuecomment-104372622) 230 | #bower_components/ 231 | 232 | # RIA/Silverlight projects 233 | Generated_Code/ 234 | 235 | # Backup & report files from converting an old project file 236 | # to a newer Visual Studio version. Backup files are not needed, 237 | # because we have git ;-) 238 | _UpgradeReport_Files/ 239 | Backup*/ 240 | UpgradeLog*.XML 241 | UpgradeLog*.htm 242 | ServiceFabricBackup/ 243 | *.rptproj.bak 244 | 245 | # SQL Server files 246 | *.mdf 247 | *.ldf 248 | *.ndf 249 | 250 | # Business Intelligence projects 251 | *.rdl.data 252 | *.bim.layout 253 | *.bim_*.settings 254 | *.rptproj.rsuser 255 | 256 | # Microsoft Fakes 257 | FakesAssemblies/ 258 | 259 | # GhostDoc plugin setting file 260 | *.GhostDoc.xml 261 | 262 | # Node.js Tools for Visual Studio 263 | .ntvs_analysis.dat 264 | node_modules/ 265 | 266 | # Visual Studio 6 build log 267 | *.plg 268 | 269 | # Visual Studio 6 workspace options file 270 | *.opt 271 | 272 | # Visual Studio 6 auto-generated workspace file (contains which files were open etc.) 273 | *.vbw 274 | 275 | # Visual Studio LightSwitch build output 276 | **/*.HTMLClient/GeneratedArtifacts 277 | **/*.DesktopClient/GeneratedArtifacts 278 | **/*.DesktopClient/ModelManifest.xml 279 | **/*.Server/GeneratedArtifacts 280 | **/*.Server/ModelManifest.xml 281 | _Pvt_Extensions 282 | 283 | # Paket dependency manager 284 | .paket/paket.exe 285 | paket-files/ 286 | 287 | # FAKE - F# Make 288 | .fake/ 289 | 290 | # JetBrains Rider 291 | .idea/ 292 | *.sln.iml 293 | 294 | # CodeRush 295 | .cr/ 296 | 297 | # Python Tools for Visual Studio (PTVS) 298 | __pycache__/ 299 | *.pyc 300 | 301 | # Cake - Uncomment if you are using it 302 | # tools/** 303 | # !tools/packages.config 304 | 305 | # Tabs Studio 306 | *.tss 307 | 308 | # Telerik's JustMock configuration file 309 | *.jmconfig 310 | 311 | # BizTalk build output 312 | *.btp.cs 313 | *.btm.cs 314 | *.odx.cs 315 | *.xsd.cs 316 | 317 | # OpenCover UI analysis results 318 | OpenCover/ 319 | 320 | # Azure Stream Analytics local run output 321 | ASALocalRun/ 322 | 323 | # MSBuild Binary and Structured Log 324 | *.binlog 325 | 326 | # NVidia Nsight GPU debugger configuration file 327 | *.nvuser 328 | 329 | # MFractors (Xamarin productivity tool) working folder 330 | .mfractor/ 331 | 332 | # local console app that is used for sanity testing, no need to be in repo 333 | /Types/ScratchPad 334 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2019 Storm Muller 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Fixed Point Types 2 | 3 | [![Build status](https://ci.appveyor.com/api/projects/status/t6scmo444o1p56gp/branch/master?svg=true)](https://ci.appveyor.com/project/stormmuller/fixed-point-types/branch/master) 4 | [![codecov](https://codecov.io/gh/stormmuller/fixed-point-types/branch/master/graph/badge.svg)](https://codecov.io/gh/stormmuller/fixed-point-types) 5 | 6 | A library created to provide fixed point numeric types for use cases where floating point numbers just won't cut it. 7 | 8 | [Jump straight to usage](#Usage) 9 | 10 | 11 | ## When would you want to use fixed point numbers over floating point numbers? 12 | * Are fixed point numbers faster than floating point numbers? 13 | > Not really, dedicated FPUs in most modern processors actually make floating point operations faster than fixed point operations. 14 | * Are fixed point numbers more precise than floating point numbers? 15 | > No, sometimes fixed point numbers are less precise than floating point numbers. But also this can vary based on the scale the developer chooses. 16 | * Do fixed point numbers have a larger range than floating point numbers? 17 | > Nope, not when using the same amount of bits when storing the fixed point number anyway. This is actually a huge advantage of floating point numbers. 18 | * So why would you want to use fixed point numbers? 19 | > Fixed point numbers are deterministic. This is really useful for things like simulations where results need to be perfectly replicable. I originally created this library to create a game using a deterministic lockstep networking model. But There are many different applications for fixed point numbers. 20 | 21 | ## Usage 22 | 23 | Install the [nuget package](https://www.nuget.org/packages/FixedTypes/) 24 | 25 | OR 26 | Include the `Fixed32.cs`(Or the entire `FixedTypes.csproj`) file in your solution. 27 | 28 | To initialize a new fixed point number: 29 | ```csharp 30 | var someDeterministicNumber = new Fixed32(16); // 0.0 31 | var someDeterministicNumber = new Fixed32(16, 5); // 5.0 32 | ``` 33 | 34 | The 16 passed to constructor is the scale of the number. It basically specifies how many bits should be used to store the **characteristic**(bits preceding the point) and how many bits should be used to store the **mantissa**(bits succeeding the point). 35 | 36 | ### :information_source: Tips 37 | * A smaller scale will decrease precision and increase range. 38 | * A larger scale will increase precision and decrease range. 39 | * Scale should be between(and including) 1 and 31 40 | * Operations +, -, *, / etc. should only be done on fixed point numbers of the same scale. 41 | 42 | There is no way to initialize a fixed point number as a fraction(only as a whole number). This is because all numeric literals in C# are floating point numbers, with the exception of integers. If you wanted to initialize a fraction I would suggest dividing the numerator with denominator of the fraction. For example: 43 | ```csharp 44 | var twoAndAHalf = new Fixed32(16, 5) / new Fixed32(16, 2); // 5/2 = 2.5 45 | ``` 46 | 47 | ## Contributing 48 | 49 | This project is covered by many tests, if you find a bug please create an issue. If you know how to solve the issue please create a pull request with the solution and a test for the scenario described in the original issue. 50 | 51 | This library is far from complete and still requires many useful features. So feel free to create a pull request.:v: 52 | -------------------------------------------------------------------------------- /Types/Tests/FixedNumberTests.cs: -------------------------------------------------------------------------------- 1 | namespace FixedTypes.Tests 2 | { 3 | using NUnit.Framework; 4 | using Types; 5 | 6 | [TestFixture] 7 | public class FixedNumberTests 8 | { 9 | [Test] 10 | public void DefaultsTo0RawValue() 11 | { 12 | var number = new Fixed32(16); 13 | 14 | Assert.AreEqual(0, number.RawValue); 15 | } 16 | 17 | [Test] 18 | public void WholeNumberConstructorAddsWholeNumber() 19 | { 20 | var number = new Fixed32(16, 1); 21 | 22 | Assert.AreEqual(1, number.WholeNumber); 23 | } 24 | 25 | [Test] 26 | public void WholeNumberConstructorDoesNotAddFraction() 27 | { 28 | var number = new Fixed32(16, 1); 29 | 30 | Assert.AreEqual(0, number.Fraction); 31 | } 32 | 33 | [Test] 34 | public void AddsWholeNumbers() 35 | { 36 | var leftHandSide = new Fixed32(16, 2); 37 | var rightHandSide = new Fixed32(16, 3); 38 | 39 | var result = leftHandSide + rightHandSide; 40 | 41 | Assert.AreEqual(5, result.WholeNumber); 42 | Assert.AreEqual(0, result.Fraction); 43 | } 44 | 45 | [Test] 46 | public void SubrtactsWholeNumbers() 47 | { 48 | var leftHandSide = new Fixed32(16, 2); 49 | var rightHandSide = new Fixed32(16, 3); 50 | 51 | var result = leftHandSide - rightHandSide; 52 | 53 | Assert.AreEqual(-1, result.WholeNumber); 54 | Assert.AreEqual(0, result.Fraction); 55 | } 56 | 57 | [Test] 58 | [TestCase(1, 2, 2, 2.5d)] 59 | [TestCase(3, 2, 2, 3.5d)] 60 | [TestCase(-3, 2, 2, 0.5d)] 61 | [TestCase(1, 6, 84, 84.1666666666666667d)] 62 | public void AddsFractionalNumbers(int fractionNumerator, int fractionDenominator, int wholeNumberToAddTo, double expected) 63 | { 64 | var leftHandSide = new Fixed32(16, fractionNumerator) / new Fixed32(16, fractionDenominator); 65 | var rightHandSide = new Fixed32(16, wholeNumberToAddTo); 66 | 67 | var result = leftHandSide + rightHandSide; 68 | 69 | Assert.That((double)result, Is.EqualTo(expected).Within(0.0001)); 70 | } 71 | 72 | [Test] 73 | [TestCase(1, 2, 2, -1.5d)] 74 | [TestCase(3, 2, 2, -0.5d)] 75 | [TestCase(-3, 2, 2, -3.5d)] 76 | [TestCase(1, 6, 84, -83.83333333333333d)] 77 | public void SubtractsFractionalNumbers(int fractionNumerator, int fractionDenominator, int wholeNumberToAddTo, double expected) 78 | { 79 | var leftHandSide = new Fixed32(16, fractionNumerator) / new Fixed32(16, fractionDenominator); 80 | var rightHandSide = new Fixed32(16, wholeNumberToAddTo); 81 | 82 | var result = leftHandSide - rightHandSide; 83 | 84 | Assert.That((double)result, Is.EqualTo(expected).Within(0.0001)); 85 | } 86 | 87 | [Test] 88 | public void ConvertsToDouble() 89 | { 90 | var leftHandSide = new Fixed32(16, 1); 91 | 92 | Assert.AreEqual(1d, (double)leftHandSide); 93 | } 94 | 95 | [Test] 96 | public void MultipliesWholeNumbers() 97 | { 98 | var leftHandSide = new Fixed32(16, 9); 99 | var rightHandSide = new Fixed32(16, 4); 100 | 101 | var result = leftHandSide * rightHandSide; 102 | 103 | Assert.AreEqual(36, result.WholeNumber); 104 | } 105 | 106 | [Test] 107 | [TestCase(14, 2, 7)] 108 | [TestCase(2, 2, 1)] 109 | [TestCase(-2, 2, -1)] 110 | [TestCase(-14, 2, -7)] 111 | [TestCase(36, 4, 9)] 112 | [TestCase(-36, 4, -9)] 113 | [TestCase(36, -4, -9)] 114 | public void DividesWholeNumbers(int number1, int number2, int expectedResult) 115 | { 116 | var numerator = new Fixed32(16, number1); 117 | var denomenator = new Fixed32(16, number2); 118 | 119 | var result = numerator / denomenator; 120 | 121 | Assert.AreEqual(expectedResult, result.WholeNumber); 122 | } 123 | 124 | [Test] 125 | [TestCase(1, 2, 0.5d)] 126 | [TestCase(-1, 2, -0.5d)] 127 | [TestCase(1, -2, -0.5d)] 128 | [TestCase(-1, -2, 0.5d)] 129 | [TestCase(22, 7, 3.142857142857143d)] 130 | [TestCase(1, 3, 0.333333333333334d)] 131 | public void DividesFractionalNumbers(int number1, int number2, double expectedResult) 132 | { 133 | var numerator = new Fixed32(16, number1); 134 | var denomenator = new Fixed32(16, number2); 135 | 136 | var result = numerator / denomenator; 137 | 138 | Assert.That((double)result, Is.EqualTo(expectedResult).Within(0.0001)); 139 | } 140 | 141 | [Test] 142 | [TestCase(-1, 2, 0)] 143 | [TestCase(1, -2, 0)] 144 | [TestCase(22, 7, 3)] 145 | [TestCase(22, -7, -3)] 146 | public void CalculatesWholeNumberCorrectlyWhenNumberIdFraction(int number1, int number2, int expectedResult) 147 | { 148 | var numerator = new Fixed32(16, number1); 149 | var denomenator = new Fixed32(16, number2); 150 | 151 | var result = numerator / denomenator; 152 | 153 | Assert.AreEqual(expectedResult, result.WholeNumber); 154 | } 155 | 156 | [Test] 157 | [TestCase(1, 2, 0)] 158 | [TestCase(-1, 2, 0)] 159 | [TestCase(1, -2, 0)] 160 | [TestCase(-1, -2, 0)] 161 | [TestCase(22, 7, 3)] 162 | [TestCase(22, -7, -3)] 163 | [TestCase(1, 3, 0)] 164 | public void ImplicitIntegerOperatorReturnsWholeNumber(int number1, int number2, int expectedResult) 165 | { 166 | var numerator = new Fixed32(16, number1); 167 | var denomenator = new Fixed32(16, number2); 168 | 169 | var result = numerator / denomenator; 170 | int integerValue = result; 171 | 172 | Assert.AreEqual(result.WholeNumber, integerValue); 173 | Assert.AreEqual(expectedResult, integerValue); 174 | } 175 | 176 | [Test] 177 | [TestCase(1)] 178 | [TestCase(112)] 179 | [TestCase(-1)] 180 | [TestCase(-112)] 181 | [TestCase(1000)] 182 | public void ImplicitFixedOperatorCorrectlyCreatesNumber(int integerToCreateWith) 183 | { 184 | Fixed32 number = integerToCreateWith; 185 | 186 | Assert.AreEqual(integerToCreateWith, number.WholeNumber); 187 | Assert.AreEqual(0, number.Fraction); 188 | } 189 | } 190 | } 191 | -------------------------------------------------------------------------------- /Types/Tests/FixedTypes.Tests.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | netcoreapp2.1 5 | false 6 | full 7 | true 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | -------------------------------------------------------------------------------- /Types/Types.sln: -------------------------------------------------------------------------------- 1 |  2 | Microsoft Visual Studio Solution File, Format Version 12.00 3 | # Visual Studio 15 4 | VisualStudioVersion = 15.0.28010.2036 5 | MinimumVisualStudioVersion = 10.0.40219.1 6 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "FixedTypes", "Types\FixedTypes.csproj", "{8B211BE8-C758-46CB-8E93-A276A2959FF6}" 7 | EndProject 8 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "FixedTypes.Tests", "Tests\FixedTypes.Tests.csproj", "{B603875C-7296-4FD4-8D12-71A572D1833F}" 9 | EndProject 10 | Global 11 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 12 | Debug|Any CPU = Debug|Any CPU 13 | Release|Any CPU = Release|Any CPU 14 | EndGlobalSection 15 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 16 | {8B211BE8-C758-46CB-8E93-A276A2959FF6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 17 | {8B211BE8-C758-46CB-8E93-A276A2959FF6}.Debug|Any CPU.Build.0 = Debug|Any CPU 18 | {8B211BE8-C758-46CB-8E93-A276A2959FF6}.Release|Any CPU.ActiveCfg = Release|Any CPU 19 | {8B211BE8-C758-46CB-8E93-A276A2959FF6}.Release|Any CPU.Build.0 = Release|Any CPU 20 | {B603875C-7296-4FD4-8D12-71A572D1833F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 21 | {B603875C-7296-4FD4-8D12-71A572D1833F}.Debug|Any CPU.Build.0 = Debug|Any CPU 22 | {B603875C-7296-4FD4-8D12-71A572D1833F}.Release|Any CPU.ActiveCfg = Release|Any CPU 23 | {B603875C-7296-4FD4-8D12-71A572D1833F}.Release|Any CPU.Build.0 = Release|Any CPU 24 | EndGlobalSection 25 | GlobalSection(SolutionProperties) = preSolution 26 | HideSolutionNode = FALSE 27 | EndGlobalSection 28 | GlobalSection(ExtensibilityGlobals) = postSolution 29 | SolutionGuid = {1D898CCF-A46A-4175-A0B6-864B6B971D68} 30 | EndGlobalSection 31 | EndGlobal 32 | -------------------------------------------------------------------------------- /Types/Types/Fixed32.cs: -------------------------------------------------------------------------------- 1 | namespace Types 2 | { 3 | public struct Fixed32 4 | { 5 | public readonly int Scale; 6 | public const int Epsilon = 1; 7 | 8 | private const int FractionMask = 0xffff; 9 | private const int DefaultScale = 16; 10 | 11 | public long RawValue { get; private set; } 12 | 13 | public Fixed32(int scale) : this(scale, 0) { } 14 | 15 | public Fixed32(int scale, int wholeNumber) 16 | { 17 | this.Scale = scale; 18 | this.RawValue = wholeNumber << scale; 19 | } 20 | 21 | public int WholeNumber 22 | { 23 | get 24 | { 25 | return (int)(this.RawValue >> this.Scale) + 26 | (this.RawValue < 0 && this.Fraction != 0 ? 1 : 0); 27 | } 28 | } 29 | 30 | public int Fraction 31 | { 32 | get 33 | { 34 | return (int)(this.RawValue & FractionMask); 35 | } 36 | } 37 | 38 | public static Fixed32 operator +(Fixed32 leftHandSide, Fixed32 rightHandSide) 39 | { 40 | leftHandSide.RawValue += rightHandSide.RawValue; 41 | 42 | return leftHandSide; 43 | } 44 | 45 | public static Fixed32 operator -(Fixed32 leftHandSide, Fixed32 rightHandSide) 46 | { 47 | leftHandSide.RawValue -= rightHandSide.RawValue; 48 | 49 | return leftHandSide; 50 | } 51 | 52 | public static Fixed32 operator *(Fixed32 leftHandSide, Fixed32 rightHandSide) 53 | { 54 | var result = leftHandSide.RawValue * rightHandSide.RawValue; 55 | 56 | leftHandSide.RawValue = result >> leftHandSide.Scale; 57 | 58 | return leftHandSide; 59 | } 60 | 61 | public static Fixed32 operator /(Fixed32 leftHandSide, Fixed32 rightHandSide) 62 | { 63 | var result = (leftHandSide.RawValue << leftHandSide.Scale) / rightHandSide.RawValue; 64 | 65 | leftHandSide.RawValue = result; 66 | 67 | return leftHandSide; 68 | } 69 | 70 | public static explicit operator double(Fixed32 number) 71 | { 72 | return (double)number.RawValue / (1 << number.Scale); 73 | } 74 | 75 | public static implicit operator int(Fixed32 number) 76 | { 77 | return number.WholeNumber; 78 | } 79 | 80 | public static implicit operator Fixed32(int number) 81 | { 82 | return new Fixed32(DefaultScale, number); 83 | } 84 | 85 | public override string ToString() 86 | { 87 | return ((double)this).ToString(); 88 | } 89 | } 90 | } 91 | -------------------------------------------------------------------------------- /Types/Types/FixedTypes.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | netstandard2.0 5 | full 6 | 0.0.1 7 | true 8 | false 9 | Storm Muller 10 | 11 | A C# library for fixed point numbers 12 | FixedTypes (c) 2019, Storm Muller All rights reserved 13 | https://github.com/stormmuller/fixed-point-types/ 14 | https://raw.githubusercontent.com/stormmuller/fixed-point-types/master/.github/FxPT.png 15 | https://github.com/stormmuller/fixed-point-types/ 16 | GIT 17 | Number Unity Fixed-point Lockstep Deterministic 18 | en 19 | MIT 20 | 21 | 22 | 23 | --------------------------------------------------------------------------------