wp_send_json* functions do a weird float values conversion - ajax

I have a method that does simple math and returns a float value. I use this value as an argument for wp_send_json() function. Values are converted in a way that I don't understand.
$x = $calculator->getValue(); // Returns 3.02
$y = 3.02;
gettype($x); // Returns double
gettype($y); // Returns bouble
wp_send_json_success(%x); // prints {"success":false,"data":3.0199999999999996}
wp_send_json_success($y); // prints {"success":false,"data":3.02}
I don't' understand it at all. Both $x and $y values are exactly the same in terms of values and types. So why the outputs produced by wp_send_json_success are different?
Update:
Now I'm even more confused
$x === $y; // returns false
$x == $y; // returns false too!
How is it possible? When I do var_dump I can see that the values are the same.

It has nothing to do with wp_send_json(). In my code the 3.02 value returned by the $calculator->getValue() method is a result of a 10 - 6.98 equation. It returns 3.0199999999999996 but when I var_dump it then the result on the screen is rounded to 3.02. That's why $x == $y returns false.

Related

XQuery concat to the same variable

Simple code there-
let $sV2 :=''
for $i in (1 to 2)
let $sV1 := 'test1'
let $sV2 := if (fn:string-length($sV2) != 0) then fn:concat($sV2,'||',$sV1) else ($sV1)
return
<test>{$sV2}</test>
i get the same output
<test>test1</test>
<test>test1</test>
whereas i expect
<test>test1</test>
<test>test1||test1</test>
what am i doing wrong? i tried to debug but why does V2 initialize to null/'' for second iteration.
Note- I have to use xquery 1.0 do to some product limitations.
How do i get the expected output?
XQuery is a functional language. As such, it does not allow you to change the value of a variable once it has been assigned.
There are many other ways to reach the same results, though. Your specific query can e.g. be rewritten as follows:
for $i in 1 to 2
return <test>{
string-join(for $n in 1 to $i return 'test1', '||')
}</test>

'Return' outside of function

def suspect_dict():
dict = pd.read_csv("suspectdict.csv", squeeze=True)
pattern = '|'.join(dict)
result = np.where(news_df["headline"].str.contains(pattern, na=False),1, 0)
for index, value in enumerate(result):
return {value}
I am trying to return 1 and 0 if words in "suspectdict" exists in "news_df".
Code works for
for index, value in enumerate(result):
print(f"{value}")
example of output:
0
1
0
1
1
When using return I got Syntax error: 'return' outside function
How do I fix this?
As the error says, your return is outside a function. Return should be used within a scope of a function, in your case, on suspect_dict() function.
You could either just loop the result and print it, without returning anything, also, you don't need to use enumerate as you're not dealing with indexes:
def suspect_dict():
dict = pd.read_csv("suspectdict.csv", squeeze=True)
pattern = '|'.join(dict)
result = np.where(news_df["headline"].str.contains(pattern, na=False),1, 0)
for value in result:
print(value)
But if you need to return the result, you could just use return result inside the function:
def suspect_dict():
dict = pd.read_csv("suspectdict.csv", squeeze=True)
pattern = '|'.join(dict)
result = np.where(news_df["headline"].str.contains(pattern,na=False),1,0)
return result
Note that python uses indentation to understand where a block of code begins and ends, so make sure that all codes that might belong to the function are well indented.

Where did I go wrong in this M-Code function for power-query?

I'm trying to write an M-Code function that I can use to filter out text values that aren't of the format "A0000" or "B0000" or "C0000" etc.
I tried my hand at writing a custom function in Power-Query, but the nuances of this language seem to escape me and the documentation and the syntax error warnings are pretty much useless in troubleshooting.
Here's what I have so far. I'm getting an error "Token Identifier Expected" which highlights the very first if line when I click the show error button.
Does anyone know where I've gone wrong here, and what I might try to fix it?
let IsNewPN= (Argument) =>
let
/*M code to evaluate in the function goes here*/
a= Text.Start(Argument,1),
b= Text.End(Argument, Text.Length(Argument) - 1),
if a="A" or a="B" or a="C" or a="D" or a="E" or a="F" or a="H" or a="M" or a="Q" or a="W"
then x=1
else x=0
if x=0
then Result = "FALSE"
else Result=try a & Text.InferNumberType(b) otherwise "FALSE"
in
Result
in
IsNewPN
Each line needs to be of the form value = expression.
Try changing your code to this:
let IsNewPN= (Argument) =>
let
/*M code to evaluate in the function goes here*/
a= Text.Start(Argument,1),
b= Text.End(Argument, Text.Length(Argument) - 1),
x =
if a="A" or a="B" or a="C" or a="D" or a="E" or a="F" or a="H" or a="M" or a="Q" or a="W"
then 1
else 0,
Return =
if x=0
then "FALSE"
else try a & Text.InferNumberType(b) otherwise "FALSE"
in
Result
in
IsNewPN
In this code, x and Return are the 'token identifiers` for their respective lines.
Side note: You can simplify your big or expression using this instead:
Text.Contains("ABCDEFHMQW",a)

Pass an array to a function and output each line

I was wondering if anybody could help.
I have the below code, it has an array of variables, arr, this is being passed to the method outputArray(), I require this to output each of the individual elements on their own line, when I run this code it only outputs the value of var1 and then finishes executing. The function does return a 0 or 1 (true or false) but this is functioning as expected anyway.
function outputArray() {
for i in "$#"; do
echo $i
# Condition omitted, this would manipulate data and return either 1 or 0
}
#Variable definition omitted
arr=($var1 $var2 $var3)
if outputArray "${arr[#]}"; then
echo "True"
fi
I hope this makes sense, I don't post here often so please let me know if not and I'll try again.

XQuery let for let output

I have the following XQuery:
let $a := 0
for $b in (1,2,3)
let $a := $a + 1
return $a+$b
The result I would expect is 2,4,6
However, the result is get is 2,3,4
Why does it produce this result, i.e. why does the value of $a in the for loop stays 1?
Variables in XQuery (and XSLT) are immutable. Therefore, once declared they cannot be reassigned.

Resources