go error % not allowed - go

I have written a program for Mumax with a go syntax but I don't understant my error. Here the script where the error appears :
n:=0
Dtr0:=5*1e-12
Dtd0 :=300*1e-12
Dtf0:=5*1e-12
Dtz0:=20000*1e-12
tr0:=Dtr0
td0:=Dtd0+tr0
tf0:=Dtf0+td0
tz0:=Dtz0+tf0
TT:=tz0
n=t/TT
tr:=tr0+(n*TT)
td:=td0+(n*TT)
tf:=tf0+(n*TT)
tz:=tz0+(n*TT)
if (n % 2 == 0) {
if (n<1 && t<tr) {
a:=(t/tr)
} else if (n>=1 && t>=tz0+((n-1)*TT) && t<tr) {
a:=1/(tr-(tz0+((n-1)*TT)))*(t-(tz0+((n-1)*TT)))
} else if (t>=tr && t<=td) {
a:=1
} else if (t>td && t<=tf) {
a:=(-1/(tf-td))*(t-td)+1
} else if (t>tf && t<tz) {
a:=0
}
}
if (int(n)%2==1) {
if (n<1 && t<tr) {
a:=-(t/tr)
} else if (n>=1.0 && t>=tz0+((n-1)*TT) && t<tr) {
a:=-(1/(tr-(tz0+((n-1)*TT)))*(t-(tz0+((n-1)*TT))))
} else if (t>=tr && t<=td) {
a:=-1
} else if (t>td && t<=tf) {
a:=-((-1/(tf-td))*(t-td)+1)
} else if (t>tf && t<tz) {
a:=0
}
}
And the error message is : line 37: if (n % 2 == 0) {: not allowed: %
Thank's a lot

There are two problems here:
n must be a float because TT must be a float, because that's ultimately a function of two floats. This conflicts with the n := 0 default int definition at the top.
the modulus operator on floats is undefined (see this playground for what happens when you try).
This means you have a very weird Go implementation or we're not seeing everything.
In any case, either you have to either coerce n to an int (as you do in your second if) or use math.Mod somehow.

Related

Error with fread() in c, not having expected outut

I'm making a checksum algorithm for one of my classes, I want to read two binary files and run them through a checksum algorithm. The checksum algorithm works (I've tried inputting what I want into the terminal and it works) but I can't get my fread() to work. I've tried printing the outputs and they print the correct stuff, but then a bunch of other random numbers and letters at the end.
Here is my code:
int main(int argc, char *argv[])
{
FILE *ptr1;
FILE *ptr2;
ptr1 = fopen("test1.bin","rb");
ptr2 = fopen("test2.bin","rb");
char file1[sizeof(ptr1)], file2[sizeof(ptr2)];
char sum[sizeof(ptr1)], comp[sizeof(ptr1)];
fread(file1,sizeof(file1),1,ptr1);
fread(file2,sizeof(file2),1,ptr2);
fclose(ptr1);
fclose(ptr2);
/* char file1[20], file2[20];
char sum[20], comp[20];
printf("enter 1\n");
scanf("%s",&file1);
printf("enter 2\n");
scanf("%s",&file2);*/
if(strlen(file1)==strlen(file2)) {
char next='0';
int length = strlen(file1);
for(int i=length-1;i>=0;i--)
{
if(file1[i]=='0' && file2[i]=='0' && next=='0')
{
sum[i]='0';
next='0';
}
else if(file1[i]=='0' && file2[i]=='0' && next=='1')
{
sum[i]='1';
next='0';
}
else if(file1[i]=='0' && file2[i]=='1' && next=='0')
{
sum[i]='1';
next='0';
}
else if(file1[i]=='0' && file2[i]=='1' && next=='1')
{
sum[i]='0';
next='1';
}
else if(file1[i]=='1' && file2[i]=='0' && next=='0')
{
sum[i]='1';
next='0';
}
else if(file1[i]=='1' && file2[i]=='0' && next=='1')
{
sum[i]='0';
next='1';
}
else if(file1[i]=='1' && file2[i]=='1' && next=='0')
{
sum[i]='0';
next='1';
}
else if(file1[i]=='1' && file2[i]=='1' && next=='1')
{
sum[i]='1';
next='1';
}
else
break;
}
for (int i=0;i<length;i++)
{
if(sum[i]=='0')
comp[i]='1';
else
comp[i]='0';
}
if(next=='1')
next='0';
else
next='1';
printf("\nChecksum=%c%s",next, comp);
}
else {
printf("\nInput Lengths do not match");
}
}
test1.bin and test2.bin are two files that contain 8 bytes of binary. I've tried using
printf("this is file 1 %s\n", file1)
printf("this is file 2 %s\n", file2)
to help debug and it outputs
this is file 1 01001001dL
this is file 2 01001000P5L
What is my error here? I'm not great at C so I'm sure its something simple.
You allocate sizeof(ptr1) bytes for file1, but that means the size of the type FILE*, which is likely to be 4. If you know your file contains exactly 8 bytes, do write 8 there.

PVS-Studio: warning V595 is generated even if pointer is checked

In the following code there is already check for nullptr in (1):
int msg;
struct x * var[2];
if ((var[0] = get_x()) == nullptr) { // (1)
return;
}
if (var[0]->data != 11) { // (2) <<< V595
msg = 1;
printf("msg1");
}
if (var[0] && var[0]->data == 12) { // (3) <<< V595
msg = 2;
return;
}
but I still get error: V595. Why?
I agree that there is an exceeding check for nullptr in (3).
Analyzer considers this piece of code abnormal. First, the pointer is being deferenced, and after that it is being verified. Even if it cannot be equal to NULL, it looks very suspicious. There's a possibility that wrong variable is used or checked.
So it is possible that the wrong variable is used, and the corrected version of code could look like:
if (FOO->data != 11) {
msg = 1;
printf("msg1");
}
if (var[0] && var[0]->data == 12) {
msg = 2;
return;
}
Or, probably, the condition is incorrect:
if (var[0]->data != 11) {
msg = 1;
printf("msg1");
}
if (FOO && var[0]->data == 12) {
msg = 2;
return;
}
Anyway, the analyzer doesn't like it, and it issues a warning. To eliminate such warnings, remove unnecessary checks which overload the code and confuse other programmers and the analyzer. In this case the analyzer will not issue the warning:
if ((var[0] = get_x()) == nullptr) {
return;
}
if (var[0]->data != 11) {
msg = 1;
printf("msg1");
}
if (var[0]->data == 12) {
msg = 2;
return;
}
If you don't want to remove this check, use one of the following ways to suppress warnings:
Suppression of false alarms
Mass Suppression of Analyzer Messages

Slickgrid sorting when using DataView and expand/collapse

I've been doing a LOT of googling, but couldn't find a way to implement this. Sorry if I missed it in my searching.
I used Example 4 adding tree functionality as a template, but I've been unable to implement column sorting while tree functionality is active.
Everything I've tried winds up in an infinite loop in the rendered version of my inline filter due to the IDs no longer being in order.
Has anyone been able to implement sorting with tree functionality or have any pointers how it can be accomplished? Is there a way to sort the parent rows and leave the parents and children together?
My filter is basically the same as the one in the example with some minor tweaks to the search.
It gets stuck looping the item.parent if since the parents and children are no longer sequential. Now all rows have children.
if (item.parent != null) {
var parent = oppLineGridData[item.parent];
while (parent) {
if (parent._collapsed) {
return false;
}
parent = oppLineGridData[parent.parent];
}
}
Full function:
function openFilter(item) {
if (specificColumn != null) {
if (searchString != "" && item[specificColumn].toLowerCase().indexOf(searchString) == -1) {
return false;
}
} else {
if (searchString != ""
&& item["accountName"].toLowerCase().indexOf(searchString) == -1
&& item["solution"].toString().toLowerCase().indexOf(searchString) == -1
&& item["Adjusted_Commitment__c"].toLowerCase().indexOf(searchString) == -1
&& item["Deal_Registration_ID__c"].toLowerCase().indexOf(searchString) == -1
&& item["lineItemValue"].toString().toLowerCase().indexOf(searchString) == -1
&& item["oppotunityName"].toLowerCase().indexOf(searchString) == -1
&& item["closeDate"].toLowerCase().indexOf(searchString) == -1
&& item["productName"].toLowerCase().indexOf(searchString) == -1
&& item["stageName"].toLowerCase().indexOf(searchString) == -1
&& item["ownerName"].toLowerCase().indexOf(searchString) == -1
&& item["accountManagerList"].toLowerCase().indexOf(searchString) == -1
&& item["accountManagerMgr2"].toLowerCase().indexOf(searchString) == -1
&& item["ProServicesEngagement"].toLowerCase().indexOf(searchString) == -1
&& item["proServicesEngagementAll"].toLowerCase().indexOf(searchString) == -1
&& item["productType"].toLowerCase().indexOf(searchString) == -1
&& item["commissionableEMName"].toLowerCase().indexOf(searchString) == -1
&& item["lastUpdated"].toLowerCase().indexOf(searchString) == -1
&& item["updatedBy"].toLowerCase().indexOf(searchString) == -1) {
return false;
}
}
if (item.parent != null) {
var parent = oppLineGridData[item.parent];
while (parent) {
if (parent._collapsed) {
return false;
}
parent = oppLineGridData[parent.parent];
}
}
return true;
}
The only answer is to disable the inlining of filters. In other words, the option inlineFilters should be set to false.
I just suffered from the same issue and I tell you this from the experience. The inlining of filters for SlickGrid won't handle complex code.
Even a for with an if inside or a inner loop was enough for it to go into infinite loop for me.

How to simplify a simple loop in Javascript?

I am now trying it out for a while and get it perfect. I am trying to simplify this for loop I created and make it actually work, without any arrays and only the most basic of basic JavaScript.
for (var x=0;x<=1;x++) {
if (secondInput == luckyNumber || secondInput == luckyNumber2 || secondInput == luckyNumber3) {
if (thirdInput == luckyNumber || thirdInput == luckyNumber2 || thirdInput == luckyNumber3) {
if (firstInput == luckyNumber || firstInput == luckyNumber2 || firstInput == luckyNumber3) {
while (firstInput !== secondInput){
while(firstInput !== thirdInput){while(secondInput !== thirdInput) {
alert('Congratulations! You got all 3 numbers correct. You\'ve won £1,000!');
}
}
}
}
}
}
Does this code make sense or am I doing something wrong? I've got the feeling that I can even leave the loop out, but it is the only way how I think it is correct.
Write a function that takes the input, compares it to the lucky numbers and returns a boolean with the result.
Call that function in your if clauses.
I don't quite understand what you are trying to do with the while loops.
You could try using this idea to help:
[1, 3, 2].sort()
(store your questions and answers in arrays, and sort both then compare. Of course, checking javascript arrays for equality is a fun new project :) )
Here you go. You said you wanted it simplified.
for (var x = 0; 1 >= x; x++) {
if (!(secondInput != luckyNumber && secondInput != luckyNumber2 && secondInput != luckyNumber3 || thirdInput != luckyNumber && thirdInput != luckyNumber2 && thirdInput != luckyNumber3 || firstInput != luckyNumber && firstInput != luckyNumber2 && firstInput != luckyNumber3)) {
while (firstInput !== secondInput) {
while (firstInput !== thirdInput) {
while (secondInput !== thirdInput) {
alert("Congratulations! You got all 3 numbers correct. You\'ve won £1,000!");
}
}
}
}
}

text box percentage validation in javascript

How can we do validation for percentage numbers in textbox .
I need to validate these type of data
Ex: 12-3, 33.44a, 44. , a3.56, 123
thanks in advance
sri
''''Add textbox'''''
<asp:TextBox ID="PERCENTAGE" runat="server"
onkeypress="return ispercentage(this, event, true, false);"
MaxLength="18" size="17"></asp:TextBox>
'''''Copy below function as it is and paste in tag..'''''''
<script type="text/javascript">
function ispercentage(obj, e, allowDecimal, allowNegative)
{
var key;
var isCtrl = false;
var keychar;
var reg;
if (window.event)
{
key = e.keyCode;
isCtrl = window.event.ctrlKey
}
else if (e.which)
{
key = e.which;
isCtrl = e.ctrlKey;
}
if (isNaN(key)) return true;
keychar = String.fromCharCode(key);
// check for backspace or delete, or if Ctrl was pressed
if (key == 8 || isCtrl)
{
return true;
}
ctemp = obj.value;
var index = ctemp.indexOf(".");
var length = ctemp.length;
ctemp = ctemp.substring(index, length);
if (index < 0 && length > 1 && keychar != '.' && keychar != '0')
{
obj.focus();
return false;
}
if (ctemp.length > 2)
{
obj.focus();
return false;
}
if (keychar == '0' && length >= 2 && keychar != '.' && ctemp != '10') {
obj.focus();
return false;
}
reg = /\d/;
var isFirstN = allowNegative ? keychar == '-' && obj.value.indexOf('-') == -1 : false;
var isFirstD = allowDecimal ? keychar == '.' && obj.value.indexOf('.') == -1 : false;
return isFirstN || isFirstD || reg.test(keychar);
}
</script>
You can further optimize this expression. Currently its working for all given patterns.
^\d*[aA]?[\-.]?\d*[aA]?[\-.]?\d*$
If you're talking about checking that a given text is a valid percentage, you can do one of a few things.
validate it with a regex like ^[0-9]+\.?[0-9]*$ then just convert that to a floating point value and check it's between 0 and 100 (that particular regex requires a zero before the decimal for values less than one but you can adapt it to handle otherwise).
convert it to a float using a method that raises an exception on invalid data (rather than just stopping at the first bad character.
use a convoluted regex which checks for valid entries without having to convert to a float.
just run through the text character by character counting numerics (a), decimal points (b) and non-numerics (c). Provided a is at least one, b is at most one, and c is zero, then convert to a float.
I have no idea whether your environment support any of those options since you haven't actually specified what it is :-)
However, my preference is to go for option 1, 2, 4 and 3 (in that order) since I'm not a big fan of convoluted regexes. I tend to think that they do more harm than good when thet become to complex to understand in less than three seconds.
Finally i tried a simple validation and works good :-(
function validate(){
var str = document.getElementById('percentage').value;
if(isNaN(str))
{
//alert("value out of range or too much decimal");
}
else if(str > 100)
{
//alert("value exceeded");
}
else if(str < 0){
//alert("value not valid");
}
}

Resources