Rounding to nearest or up in SQLite - sqlite

I've got an existing calculation (in SQL) which contains several formulas involving rounding. Now I've got to make sure that the rounding uses the half up tie-breaking alorithm.
It appears that by default, SQLite's round function uses the half to even algorithm.
Is there a simple way to change that to half up?

The built-in round function rounds towards negative infinity.
There is no simply way to do half-to-even directly in SQL.
You'd have to install your own custom rounding function, or modify the SQLite source code:
if( n==0 && r>=0 && r<LARGEST_INT64-1 ){
r = (double)((sqlite_int64)(r+0.5));
}else if( n==0 && r<0 && (-r)<LARGEST_INT64-1 ){
r = -(double)((sqlite_int64)((-r)+0.5));
}else{
zBuf = sqlite3_mprintf("%.*f",n,r);
sqlite3AtoF(zBuf, &r, sqlite3Strlen30(zBuf), SQLITE_UTF8);
}

Related

Xquery transform string to same number but with decimal dot

I am working with xQuery version 1.0 and I'm trying to transform a string of numbers to the same number but with a decimal dot. But here's my problem. The decimal dot should be placed according to a certain element value.
The message I'm trying to transform:
<AMOUNT
<VALUE>34221</VALUE>
<NUMOFDEC>1</NUMOFDEC>
<SIGN>+</SIGN>
<CURRENCY>EUR</CURRENCY>
<DRCR>C</DRCR>
</AMOUNT>
What I'm trying to achieve:
<prefix:Rates
<prefix:Amount currency="EUR">3422.1</prefix:Amount>
</prefix:Rates>
What did I try:
<prefix:Rates>
<prefix:Amount currency="{ data(AMOUNT/CURRENCY) }">{ ((data(AMOUNT/VALUE) div 10)) }</prefix:Amount>
</prefix:Rates>
The problem with the above transformation is that it's not dynamic. But as you can see there is an element <NUMOFDEC>1</NUMOFDEC>. Can I use that value in a certain formula to place the decimal dot according to this value?
EDIT (19th october 2022):
As a another user mentioned, there is recursion. Let's take this recursion from user #Michael Kay:
declare function f:two-to-the($n as xs:integer) as xs:integer {
if ($n = 0) then 1 else 2 * f:two-to-the($n - 1)
};
So how will I be able to apply this to my situation?
(div math:pow(10, AMOUNT/NUMOFDEC)) might do but I don't recall whether XQuery 1 supports the math namespace mathematical functions, namespace is e.g. math="http://www.w3.org/2005/xpath-functions/math", it might depend on your XQuery processor, I guess.
So I found a solution with an if-else statement. Unfortunately as another user mentioned, the mat:pow fucntion is not supported by Xquery 1.0.
Solution:
<prefix:Amount>{
(for $decimal in (data(AMOUNT/NUMOFDEC))
return if ($decimal = '1')
then ((data(AMOUNT/VALUE)) div 10)
else if ($decimal = '2')
then ((data(AMOUNT/VALUE)) div 100)
else ($decimal = '3')
then ((data(AMOUNT/VALUE)) div 1000)
else ((data(AMOUNT/VALUE)) div 10000))
}</prefix:Amount>
Is it fully dynamic? no. But it will do the trick for now.
EDIT (19th october 2022): The following recursion works!:
declare function xf:ten-to-the($decimal as xs:integer) as xs:integer {
if ($decimal = 0) then 1 else 10 * xf:ten-to-the($decimal - 1)
};

How does nn.CrossEntropy ' s ignore_index implemented?

In pytorch, nn.CrossEntropy has a parameter: ignore_index.
How is it implemented?
Maybe like this? "compute CE loss with torch, but not use the function nn.CrossEntropy"
The loss is simply set to 0 for the values with the ignored index.
In PyTorch, nn.CrossEntropy uses negative log likelihood.
It has several implementations, for example this one.
Perhaps these lines are the most important for you:
if (cur_target == ignore_index) {
output[index] = static_cast<scalar_t>(0);
continue;
}

Finding peaks and valleys in each column, I'm not understanding this problem

So, to be clear, I don't need help solving this problem, as I know I have the knowledge to do it. The problem is, my English is not good enough to understand technical language, so I don't know exactly what I'm supposed to do.
I've the following excel file:
And I need to find the ''peaks and valleys'' of each column, and graph them.
I know how to use Matplotlib to graph, what I don't understand is what the problem reffers to with ''peaks and valleys''.
The result should be looking something like this:
So I'm guessing what ''peaks and valleys'' mean is the lowest and highest value of each column? If this is the case, the high value in Open column would be 24.6875, and the low would be 20.375?
By peaks and valleys it means maxima and minima so find either where the derivative of your function is equal to zero (and curvature != 0) or else where the data reaches a high (low) point then goes down (up) again
Function to count Valleys in Javascript
function countingValleys(n, s) {
let valleyCounter = 0;
let cLevel = 0;
let prevLevel =0;
for (let i =0; i<s.length; i++){
prevLevel = cLevel;
if(s[i] === "U"){
cLevel++;
}else{
cLevel--;
}
if(prevLevel === 0 && cLevel <0){
valleyCounter++;
}
}
return valleyCounter;
}

Using audio_play_sound() in a if statement GameMaker

I am trying to use the command:
audio_play_sound()
I am trying to insert it into this piece of code, so that when the player jumps, a sound plays.
if (key_jump) && (jumps > 0)
{
jumps -=1;
vsp = -jumpspeed;
}
Code that causes problem:
if (key_jump) && (jumps > 0)
{
jumps -=1;
vsp = -jumpspeed;
audio_play_sound(snd_jump)
}
Simply inserting the line into the if statement does not work, and gives the error WRONG NUMBER OF ARGUMENTS IN FUNCTION. This is rather confusing, perhaps I am using the wrong command? Thanks in advance
The problem is stated in the error, you're providing the wrong number of arguments to the audo_play_sound function.
from the docs
audio_play_sound(index, priority, loop);
As the person above states your answer is audio_play_sound(snd_jump, 1, false).

R: Iterating Over the List

I am trying to implement following algorithm in R:
Iterate(Cell: top)
While (top != null)
Print top.Value
top = top.Next
End While
End Iterate
Basically, given a list, the algorithm should break as soon as it hits 'null' even when the list is not over.
myls<-list('africa','america south','asia','antarctica','australasia',NULL,'europe','america north')
I had to add a for loop for using is.null() function, but following code is disaster and I need your help to fix it.
Cell <- function(top) {
#This algorithm examines every cell in the linked list, so if the list contains N cells,
#it has run time O(N).
for (i in 1:length(top)){
while(is.null(top[[i]]) !=TRUE){
print(top)
top = next(top)
}
}
}
You may run this function using:
Cell(myls)
You were close but there is no need to use for(...) in this
construction.
Cell <- function(top){
i = 1
while(i <= length(top) && !is.null(top[[i]])){
print(top[[i]])
i = i + 1
}
}
As you see I've added one extra condition to the while loop: i <= length(top) this is to make sure you don't go beyond the length of the
list in case there no null items.
However you can use a for loop with this construction:
Cell <- function(top){
for(i in 1:length(top)){
if(is.null(top[[i]])) break
print(top[[i]])
}
}
Alternatively you can use this code without a for/while construction:
myls[1:(which(sapply(myls, is.null))[1]-1)]
Check this out: It runs one by one for all the values in myls and prints them but If it encounters NULL value it breaks.
for (val in myls) {
if (is.null(val)){
break
}
print(val)
}
Let me know in case of any query.

Resources