' Copyright © 1998-2001, 2012-2014, 2016, 2017, 2019 John S. Philo
' This file is part of the program SVEDBERG. SVEDBERG is free software: you can redistribute it and/or modify
' it under the terms of the GNU General Public License as published by the Free Software Foundation,
' either version 3 Of the License, or any later version. The author asks only that if you use any of
' this code in your own programs then please acknowledge that use and cite any appropriate publications.
' This program is distributed in the hope that it will be useful,
' but WITHOUT ANY WARRANTY; without even the implied warranty of
' MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
' GNU General Public License for more details.
' You should have received a copy of the GNU General Public License
' along with this program. If not, see
Option Strict Off
Option Explicit On
Module LogBase10
'**** LOG10 function and routines for rounding numbers ****
'this constant is 1/log(10)
Const Logof10Inv As Double = 0.4342945
Public Function LOG10(ByRef x As Single) As Single
If x > 0 Then
LOG10 = System.Math.Log10(x)
Else
LOG10 = -40
End If
End Function
Public Sub RoundPlaces(ByRef x As Single, ByRef Places As Integer, ByRef y As Single, ByRef Digits As Long, ByRef decade As Single)
' rounds X to Places significant figures, returning
' rounded value Y, digits as value of significant figures,
' and Decade as factor to multiply digits by to get Y
Try
decade = 10.0! ^ (Int(LOG10(System.Math.Abs(x))) - Places + 1)
Digits = x / decade
y = Digits * decade
Catch ex As Exception
'do nothing on error
y = x
End Try
End Sub
Public Sub RoundDown(ByRef x As Single, ByRef Places As Integer, ByRef y As Single, ByRef Digits As Integer, ByRef decade As Single)
' rounds X DOWN to places significant figures, returning
' rounded value Y, digits as value of significant figures,
' and Decade as the factor to multiply digits by to get Y
Try
decade = 10.0! ^ (Int(LOG10(System.Math.Abs(x))) - Places + 1)
Digits = CShort(x / decade)
y = Digits * decade
Catch ex As Exception
'do nothing on error
y = x
End Try
End Sub
Public Sub RoundUp(ByRef x As Single, ByRef Places As Integer, ByRef y As Single, ByRef Digits As Integer, ByRef decade As Single)
' rounds X UP to places significant figures, returning
' rounded value Y, digits as value of significant figures,
' and Decade as the factor to multiply digits by to get Y
Try
decade = 10.0! ^ (Int(LOG10(System.Math.Abs(x))) - Places + 1)
Digits = CShort(x / decade + 0.4999999)
y = Digits * decade
Catch ex As Exception
'do nothing on error
y = x
End Try
End Sub
Public Function Rounded(ByRef X As Single, ByRef Places As Short) As Single
' returns number rounded to number of significant figures specificed by places
Dim decade, Digits As Double
Try
decade = 10.0! ^ (Int(LOG10(System.Math.Abs(X))) - Places + 1)
Digits = CInt(X / decade)
Rounded = Digits * decade
Catch ex As Exception
'do nothing on error
Rounded = X
End Try
End Function
Public Function RoundedDecimals(ByRef X As Single, ByRef Places As Short) As Single
' returns number rounded to decimals specificed by places
Dim decade, Digits As Double
Try
decade = 10.0! ^ Places
Digits = CInt(X * decade)
RoundedDecimals = Digits / decade
Catch ex As Exception
'do nothing on error
RoundedDecimals = X
End Try
End Function
End Module