Ceiling function in Access - excel

How to create a Ceiling Function in MS access that behaves the same as the one in Excel?

Since Int() seems to work like Floor(), you can get Ceiling like this:
-Int(-x)

This answer uses VBA for Access, and is derived from http://www.tek-tips.com/faqs.cfm?fid=5031:
Public Function Ceiling(ByVal X As Double, Optional ByVal Factor As Double = 1) As Double
' X is the value you want to round
' Factor is the optional multiple to which you want to round, defaulting to 1
Ceiling = (Int(X / Factor) - (X / Factor - Int(X / Factor) > 0)) * Factor
End Function
Note that this answer is mathematically correct for negative X. See http://en.wikipedia.org/wiki/Floor_and_ceiling_functions#Spreadsheet_software for background.

Thanks, marg, for the answer. For future reference, here is the VBA function that I wrote after importing the Microsoft Excel Object Library:
Public Function Ceiling(Value As Double, Significance As Double) As Double
Ceiling = Excel.WorksheetFunction.Ceiling(Value, Significance)
End Function
Then in my query, I was trying to calculate billable hours from actual time worked, rounding up to the next quarter hour:
SELECT Ceiling(([WorkTimes]![EndTime]-[WorkTimes]![BeginTime])*24,0.25) AS BillableTime
FROM WorkTimes;

You can add a Reference to the Microsoft Excel Object Library and use Excel.WorksheetFunction.Ceiling

While this question specifically asked for Access here is the answer for VB.NET
Public Function Ceiling(ByVal value As Double, ByVal factor As Double) As Double
Return Math.Ceiling(value / factor) * factor
End Function
And the answer in C#
public double Ceiling(double value, double factor)
{
return Math.Ceiling(value / factor) * factor;
}
I'm posting it here because I needed such a function google sent me to this question but I couldn't find an answer for .Net. I finally figured it out for myself.

Related

How can I add an if clause in a [vba] custom function

I wrote this function
Function potential(Volume, Original, Better)
potential = (Original - Better) * Volume
End Function
Its pretty basic but what i really need is in the case when Better is blank the answer should be 0
I tried
potential = Application.WorksheetFunction.If(Better = 0, 0, (Original - Better) * Volume)
result is always an error
or potential = ((Original - Better) * Volume) * (Better / Better) only gave an error when Better is blank
So I tried potential = Application.WorksheetFunction.IfError(((Original - Better) * Volume) * (Better / Better), 0) with the same results
What am I doing wrong here?
Use:
Function potential(ByVal Volume As Double, ByVal Original As Double, Optional ByVal Better As Double = 0) As Double
If Better = 0 Then Exit Function
potential = (Original - Better) * Volume
End Function

Is there a STDDEV equivalent in Cloud Spanner functions?

I don't see it in the documentation; is it perhaps part of an existing function I didn't notice, or available in some other way?
As Andrei Tigau mentioned, STDDEV is not supported yet. That said, you need to calculate it in two pass. Assuming you are interested in column x of YourTable,
SELECT SQRT(SUM(POW(x - avg, 2)/(n-1)))
FROM (SELECT AVG(x) AS avg, COUNT(*) AS n FROM YourTable)
CROSS JOIN YourTable;
You may try following one pass solution as well.
SELECT SQRT(s2/(n-1) - POW(s/n, 2))
FROM (
SELECT COUNT(*) AS n, SUM(x) AS s, SUM(x*x) AS s2
FROM YourTable
);
Depending on type, you may have to cast it to double (especially for s2) to avoid overflow. Both will suffer from floating point errors.
This function does not exist in the official documentation that you have send, so probably it still doesn't exist. If you need something like that you should probably calculate the standard deviation by yourself programmaticaly. You have the AVG function that helps you get at least the median value which will be helpful in the calculation of the standard deviation and the COUNT function for the number of entries.
double standardDeviation ; // standard deviation
double sumOfDiffrences = 0;
for ( int i = 0; i < count; i++ ){
sumOfDiffrences = sumOfDiffrences + pow((entry(i)-avg),2); // entry(i) is an entry of the column you want to create the S.D.
}
standardDeviation = sqrt((sumOfDiffrences)/(count-1));
STDDEV is supported since 2020-06-03.
https://cloud.google.com/spanner/docs/release-notes?hl=en#June_03_2020

How do i Square Root a Function in VBA

I am working on a MonteCarlo simulation model and part of it is to calculate the following formula:
X = Sqr(1-p)Y + Sqr(p)Z,
Where:
Y and Z are randomly obtained values based (idiosyncratic and systematic factors, respectviely) on a standard normal (inv.) distribution, calculated as:
Application.WorksheetFunction.NormInv (Rnd(), mean, sd)
p represents a correlation factor.
My aim is to square root a recalled formula, however when I try the following (inserting the first Sqr), it does not work and gives an error:
Matrix (n, sims) = (R * Sqr(Application.WorksheetFunction.NormInv(Rnd(), mean, sd))) + (Sqr(1 - R) * RandomS(s, x))
where:
R: Correlation factor
RandomS(s,x): generated matrix with Z values.
I don't want to go into too much details about the background and other variables, as the only problem I am getting is with Square Rooting the equation.
Error message I recieve reads:
Run-time error '5':
Invalid procedure call or argument
When I click debug it takes me to the formula, therefore there must be something wrong with the syntax.
Can you help with directly squaring the formula?
Thank you!
Andrew
Square root is simply Sqr.
It works fine in Excel VBA, so for example:
MsgBox Sqr(144)
...returns 12.
Just don't confuse it with the syntax for a worksheet function with is SQRT.
If you're still having an issue with your formula, tit must be with something other than the Square Root function, and I'd suggest you check the values of your variable, and make sure they are properly declared (preferably with Option Explicit at the top of the module).
Also make sure that you're passing Sqr a positive number.
Documentation: Sqr Function
I'm not a math major, but with your formula:
X = Sqr(1-p)Y + Sqr(p)Z,
...you specified how Y and Z are calculated, so calculate them separately to keep it simple:
Dim X as Double, Y as Double, Z as Double
Y = Application.WorksheetFunction.NormInv (Rnd(), mean, sd)
Z = Application.WorksheetFunction.NormInv (Rnd(), mean, sd)
Assuming the comma is not supposed to be in the formula, and having no idea what p is, your final code to calculate X is:
X = Sqr(1-p) * Y + Sqr(p) * Z

Test Visual Basic Scripts behind an Excel Spreadsheet

First of all I need to point out that I have never coded Visual Basic before.
I have an old spreadsheet which has some functions that are apparently written in Visual Basic. An example functions is:
Function Helmert_X(X, Y, Z, DX, Y_Rot, Z_Rot, s)
'Computed Helmert transformed X coordinate.
'Input: - _
cartesian XYZ coords (X,Y,Z), X translation (DX) all in meters ; _
Y and Z rotations in seconds of arc (Y_Rot, Z_Rot) and scale in ppm (s).
'Convert rotations to radians and ppm scale to a factor
Pi = 3.14159265358979
sfactor = s * 0.000001
RadY_Rot = (Y_Rot / 3600) * (Pi / 180)
RadZ_Rot = (Z_Rot / 3600) * (Pi / 180)
'Compute transformed X coord
Helmert_X = X + (X * sfactor) - (Y * RadZ_Rot) + (Z * RadY_Rot) + DX
End Function
I'm trying to convert these functions into C. I have almost finished but what I would like to do is to build a visual basic project that calls the functions with various parameters. Then I will have a C project which uses the same parameters and checks that the C gets the same answers as the Visual Basic.
When I put the Visual Basic functions into a Module in Visual Studio I get a lot of errors. Firstly the comments don't all register as comments, and the variables apparently need to be declared.
Am I using the functions properly? Is there anyway to use them in code without modifying them? Could I use Excel to run test parameters through the functions?
It seems you need to declare all variables that exist in that function. Try to add theses lines after the comments:
Dim Pi As Double
Dim sfactor As Double
Dim RadY_Rot As Double
Dim RadZ_Rot As Double
You can also specify type for variables that came with function, as well the function itself:
Function Helmert_X(X as Double, Y as Double, Z as Double, DX as Double, Y_Rot as Double, Z_Rot as Double, s as Double) as Double
Let me know if it works or if there is still some issue.

How to truncate floating point number in jscript?

How to truncate float value in Jscript?
eg.
var x = 9/6
Now x contains a floating point number and it is 1.5.
I want to truncate this and get the value as 1
x = Math.floor(x)
This will round the value of x down to the nearest integer below.
Math.round() should achieve what you're looking for, but of course it'll round 1.5 to 2. If you always want to round down to the nearest integer, use Math.floor():
var x = Math.floor(9 / 6);
Math.floor() only works as the OP intended when the number is positive, as it rounds down and not towards zero. Therefore, for negative numbers, Math.ceil() must be used.
var x = 9/6;
x = (x < 0 ? Math.ceil(x) : Math.floor(x));
Another solution could be:
var x = parseInt(9 / 6);
Wscript.StdOut.WriteLine(x); // returns 1
the main purpose of the parseInt() function is to parse strings to integers. So I guess it might be slower than Math.floor() and methods as such.

Resources