I have basic doubt.I have to convert float minutes =( (10.09/60 ) % 60);
but error is invalid to binary expression(double to double).
how can I make this calculation easy..
What I am trying is firstly
float minutes =( (10.09/60 )
then trying to convert minutes into NSInterger to solve module (%) operation..
but how can I do this...or else suggest other solution to get this calculation..
float minutes =( (10.09/60 ) % 60);
The % operator doesn't work on floating-point values. You'll have to use a function to calculate what you want. Here's the basic pseudocode of what it would look like:
B % A = B - (floor(B / A) * A)
There are also library functions which will do this for you in math.h or tgmath.h
Also, you can use:
fmod() or fmodf() from .
To use %, you have to use an int. So : (int)(10.09)%60 should work.
You can use following operations depends upon requirement
float myFloat = 3.333
//for nearest small integer:
int result = (int)ceilf(myFloat );
//for nearest big integer:
int result = (int)roundf(myFloat );
//for nearest integer:
int result = (int)floor(myFloat);
//For just an integer value
int result = (int) (myFloat);
Related
val year : Int = spinYear.getSelectedItem() as Int
selDate.text = "$year / $month / $date"
val curTime = LocalDateTime.now()
val curYear : Int = curTime.year
val difYear : Int = curYear - year
ageInMins.text = difYear.toString()
The language is Kotlin, and this is a part of my code.
The value year comes from a spinner named spinYear, and I got the selected value as an integer. Ultimately I want to substitute the text ageInMins with the value difYear.
I'm trying to define my difYear by subtracting the two variables: curYear and year, but the app crashes every time.
It seems like both of the variables I'm subtracting are of type Int, so I have no idea what to do with this problem.
If someone could give me an advice it would be great :)
I searched around Google to find information about this, but there was no useful information. I also tried to put .toInt() to the values but it didn't work. I tried every single thing I can think of, but it did not work. So I am seeking help from you guys.
I need to compute the factorial of a variable in Google BigQuery - is there a function for this? I cannot find one in the documentation here:
https://cloud.google.com/bigquery/query-reference#arithmeticoperators
My proposed solution at this point is to compute the factorial for numbers 1 through 100 and upload that as a table and join with that table. If you have something better, please advise.
As context may reveal a best solution, the factorial is used in the context of computing a Poisson probability of a random variable (number of events in a window of time). See the first equation here: https://en.wikipedia.org/wiki/Poisson_distribution
Try below. Quick & dirty example
select number, factorial
FROM js(
// input table
(select number from
(select 4 as number),
(select 6 as number),
(select 12 as number)
),
// input columns
number,
// output schema
"[{name: 'number', type: 'integer'},
{name: 'factorial', type: 'integer'}]",
// function
"function(r, emit){
function fact(num)
{
if(num<0)
return 0;
var fact=1;
for(var i=num;i>1;i--)
fact*=i;
return fact;
}
var factorial = fact(r.number)
emit({number: r.number, factorial: factorial});
}"
)
If the direct approach works for the values you need the Poisson distribution calculated on, then cool. If you reach the point where it blows up or gives you inaccurate results, then read on for numerical analysis fun times.
In general you'll get better range and numerical stability if you do the arithmetic on the logarithms, and then exp() as the final operation.
You want: c^k / k! exp(-c).
Compute its log, ln( c^k / k! exp(-c) ),
i.e. k ln(x) - ln(k!) - c
Take exp() of that.
Well but how do we get ln(k!) without computing k!? There's a function called the gamma function, whose practical point here is that its logarithm gammaln() can be approximated directly, and ln(k!) = gammaln(k+1).
There is a Javascript gammaln() in Phil Mainwaring's answer here, which I have not tested, but assuming it works it should fit into a UDF for you.
Extending Mikhail's answer to be general and correct for computing the factorial for all number 1 to n, where n < 500, the following solution holds and can be computed efficiently:
select number, factorial
FROM js(
// input table
(
SELECT
ROW_NUMBER() OVER() AS number,
some_thing_from_the_table
FROM
[any table with at least LIMIT many entries]
LIMIT
100 #Change this to any number to compute factorials from 1 to this number
),
// input columns
number,
// output schema
"[{name: 'number', type: 'integer'},
{name: 'factorial', type: 'float'}]",
// function
"function(r, emit){
function fact(num)
{
if(num<0)
return 0;
var fact=1;
for(var i=num;i>1;i--)
fact*=i;
return fact;
}
#Use toExponential and parseFloat to handle large integers in both Javascript and BigQuery
emit({number: r.number, factorial: parseFloat(fact(r.number).toExponential())});
}"
)
You can get up to 27! using SQL UDF. Above that value NUMERIC type gets overflow error.
CREATE OR REPLACE FUNCTION factorial(integer_expr INT64) AS ( (
SELECT
ARRAY<numeric>[
1,
1,
2,
6,
24,
120,
720,
5040,
40320,
362880,
3628800,
39916800,
479001600,
6227020800,
87178291200,
1307674368000,
20922789888000,
355687428096000,
6402373705728000,
121645100408832000,
2432902008176640000,
51090942171709440000.,
1124000727777607680000.,
25852016738884976640000.,
620448401733239439360000.,
15511210043330985984000000.,
403291461126605635584000000.,
10888869450418352160768000000.][
OFFSET
(integer_expr)] AS val ) );
select factorial(10);
I'm trying to get a query to work which returns a list of locId's from the database when fed a long and a lat.
Here's the sql:
eg: lat = "-37.8333300" : lon = "145.000000" : radius = (5 * 0.621371192) ^ 2
SELECT locId,longitude,latitude FROM tbliplocations WHERE (69.1*([longitude]- "&lon&") * cos("&lat&"/57.3))^2 + (69.1*([latitude]- "&lat&"))^2 < "&radius
Here's the error I receive:
The data types float and int are incompatible in the '^' operator.
I'm unsure of a workaround, can anyone point me in the right direction?
Answer:
Using SQL Server 2008 R2
SELECT city FROM tbliplocationsnew WHERE POWER((69.1*([longitude]- "&lon&") * cos("&lat&"/57.3)),2) + POWER((69.1*([latitude]- "&lat&")),2) < "&radius
Not sure what database you use, but I think that "^2" in SQL does not mean "squared" like in maths. You should use a math "power" function, like POWER(number,2) in SQL Server (since you use VB maybe you use SQL Server ?)
You need to have two of the same data type it's saying. SQL thinks "5" is an int. So, you should be able to trick it into treating it as a float, by putting "5.0" instead.
When have a issue at work where the value returned by the SUM() function isn't treated like a "normal" number when using the value returned together with the Round() function.
Try this MDX for example
WITH
MEMBER SomeNumber AS 0.595
SET SomeNumberSet AS
{[SomeNumber], [SomeNumber], [SomeNumber], [SomeNumber], [SomeNumber], [SomeNumber], [SomeNumber], [SomeNumber] }
MEMBER SomeNumberSum AS
Round(SUM([SomeNumberSet], [Measures].[SomeNumber]) / 8, 2)
SELECT [SomeNumberSum] ON 0
FROM [SomeCube]
This code returns 0.59, the sum of sets are 4,76, which are then divided by 8 = 0,595. Since MDX is using Bankers rounding this SHOULD be rounded to 0.60.
Just using Round(0,595) gives us the correct result.
Whats even more strange is that if we in the set only uses the SomeNumber 6 times or less and in the Round Function divide with the same multiplier we get 0.6 (which is correct)
Also, if I wrap the Sum() with the StrToValue() function, it works, even if I use more than 5 SomeNumbers in the set
Whats going on?!
Not sure is the actual answer you're looking for. The issue you've has to do with numerical precision, aka rounding errors, more than with MDX.
If you're in Java, run the following test :
public void testNumeric()
{
double sum = 0.0;
double value = 0.595;
for (int i = 0; i < 8; i++)
{
sum += value;
}
double prod = value * 8;
assertEquals(sum / 8, prod / 8);
}
The assert will fail, strange no ?
Result : expected:<0.5949999999999999> but was:<0.595>
The first one, sum, is how mdx is calculating the value. You got a slight difference, but it is enough for changing the result of the ROUND().
Is there a solution ?
Strictly speaking no, it's an error due to the very nature of the numeric calculation with computers. Practically you can cheat a bit round first to 10 - ROUND(ROUND(MyNumber,10),2), not brillant (10 is an example).
If you're interested start in wikipedia from here
According with Chris Webb, this behaviour is intentional:
http://www.techonthenet.com/access/functions/numeric/round.php
If you have Excel libraries, this works:
WITH
MEMBER SomeNumber AS 0.595
SET SomeNumberSet AS
{[SomeNumber], [SomeNumber], [SomeNumber], [SomeNumber], [SomeNumber], [SomeNumber], [SomeNumber], [SomeNumber] }
MEMBER SomeNumberSum AS
Excel!Round(SUM([SomeNumberSet], [Measures].[SomeNumber]) / 8, 2)
SELECT [SomeNumberSum] ON 0
FROM [Adventure Works]
I need to find a maximum of the function:
a1^x1 * const1 + a2^x2 * const2 +....+ ak^xk * constk = qaulity
where xk>0 and xk is integer. ak is constant.
constraint:
a1^x1 * const1*func(x1) + a2^x2 * const2*func(x2) +....+ ak^xk * constk*func(xk) < Budget
Where func is a discrete function:
func(x)
{
switch(x)
{
case 1: return 423;
case 2: return 544;
...
etc
}
}
k may be big(over 1000). x less then 100.
What is the best method?
There are techniques like nelder-mead optimization (which I believe GSL implements), but most techniques assume some sort of special structure (i.e. convexity or continuity). Depending on the values of the function, there may not exist a unique optimum or even an optimum that a normal downhill method can find.