Im working with Body Mass Index and im wonder why my "range" system is only setting a label to one value. Is there a better way to set this up that will work much better?
int bmiInt = currentBMI;
if ( 0<=bmiInt <= 18.5) {
weightStatus.text = #"Status: Underweight";
}
if (18.6 <= bmiInt <= 24.9) {
weightStatus.text = #"Status: Normal weight";
}
if (25 <= bmiInt <= 29.9) {
weightStatus.text = #"Status: Overweight";
}
if (bmiInt >= 30) {
weightStatus.text = #"Status: Obese";
}
For some reason weightStatus.text is always equal to #"Status Overweight" even if bmiInt is not inside that range. Why?
0 <= bmiInt <= 18.5 doesn't do what you think it does. The return value of a comparison operator is either 0 or 1, denoting true and false. This expression can be rewritten as (0 <= bmiInt) <= 18.5, which means that after evaluating the first comparison 0 <= bmiInt, you're going to end up with 0 <= 18.5 or 1 <= 18.5, which both evaluate to 1, which passes the conditional.
This is going to be true for your first 3 conditionals, which means that unless bmiInt >= 30 evaluates to true, then your label is always going to show #"Status: Overweight".
You want to rewrite this like
if (0 <= bmiInt && bmiInt <= 18.5) {
...
}
Related
I create array but cant select element.
I need array/vector with exactli this same element
[ [0,0,0] , [1,1,1] ...]
require "z3"
A = Array.new(3){|x| Z3.Int("x#{x}") }
i = Z3.Int("i")
j = Z3.Int("j")
r = Z3::Solver.new
r.assert i >= 0 && i <= 2
r.assert j >= 0 && j <= 2
r.assert r.Select(A,i) == r.Select(A,j)
r.check
p r.model
First, there's a minor syntax issue with &&. Ruby does not allow overloading of &&, so Z3 expressions need to use & and some extra parentheses:
r.assert (i >= 0) & (i <= 2)
A much bigger issue is conceptual. Do you want to use Z3 Arrays, or just plain Ruby array of Z3 Integers.
If you use Z3 arrays, then what you're asking is that some i and j exist, for which a[i] == a[j]:
require "z3"
Z3IntIntArray = Z3::ArraySort.new(Z3::IntSort.new, Z3::IntSort.new)
a = Z3IntIntArray.var("x")
i = Z3.Int("i")
j = Z3.Int("j")
r = Z3::Solver.new
r.assert (i >= 0) & (i <= 2)
r.assert (j >= 0) & (j <= 2)
r.assert a.select(i) == a.select(j)
r.check
p r.model
(upgrade to latest gem for this snippet to work)
But this could be satisfied by a model like a=[42,0,100,550], i=2, j=2.
If I run it, this returns:
Z3::Model<i=0, j=2, x=const(3)>
That is infinitely big array of all 3s, and some arbitrary i and j values. Z3 usually picks the simplest answer if it has multiple possibilities, but it could easily pick something where x[1] is a different number, as you're not really asserting anything about it.
If you use plain Ruby objects, you can specify all equalities:
require "z3"
a = (0..2).map{|i| Z3.Int("a#{i}") }
r = Z3::Solver.new
(0..2).each do |i|
(0..2).each do |j|
r.assert a[i] == a[j]
end
end
r.check
p r.model
You can save yourself O(N^2) code and just check that a[0] == a[1], a[1] == a[2] etc.:
require "z3"
a = (0..2).map{|i| Z3.Int("a#{i}") }
r = Z3::Solver.new
a.each_cons(2) do |ai, aj|
r.assert ai == aj
end
r.check
p r.model
Either of these returns:
Z3::Model<a0=0, a1=0, a2=0>
I came across this piece of code. But I am not sure how is CRC calculated here. I know the theoretical way of calculating CRC but I guess here it is using some kind of different logic, maybe. Please correct me.
r_o[14:13] <= r_o[13:12];
r_o[12] <= r_o[11]^r_o[15]^x16;
r_o[11] <= r_o[10];
r_o[10:6] <= r_o[9:5];
r_o[5] <= r_o[4]^r_o[15]^x16;
r_o[4] <= r_o[3];
r_o[3:1] <= r_o[2:0];
r_o[0] <= r_o[15]^x16;
Well, if you know about the theoretical way to do we can start with
Assuming u is the input bit, and that every iteration you calculate r = r * x + u*x^16, remember that in the given ring x^16 = x^12 + x^5 + 1, a direct implementation of this would be
parameter X16_MASK = 16'h1021; // (1 << 12) ^ (1 << 5) ^ 1
function [15:0] step(input [15:0] prev_state, input u);
logic [15:0] state;
begin
state = {prev_state[14:0], 1'b0};
if(state[15]) state = state ^ X16_MASK;
if(u) state = state ^ X16_MASK;
step = state;
end
endfunction
This could simply be written as {r_o[14:0], 1'b0} ^ (X16_MASK * (r_o[15] ^ u)) and let the synthesis to optimize whatever is necessary, it should be able to simplify the multiplication by a 1-bit signal. Now check the positions where the mask has an effect you will get to assignments above.
I would simplify
r_o[11] <= r_o[10];
r_o[10:6] <= r_o[9:5];
to r_o[11:6] = r_o[10:5]
and
r_o[4] <= r_o[3];
r_o[3:1] <= r_o[2:0];
to r_o[4:1] = r_o[3:0]
In the code you presented I am missing the assignment to r_o[15].
So you could say
r_o[15:13] <= r_o[14:12];
r_o[12] <= r_o[11]^r_o[15]^x16;
r_o[11:6] <= r_o[10:5];
r_o[5] <= r_o[4]^r_o[15]^x16;
r_o[4:1] <= r_o[3:0];
r_o[0] <= r_o[15]^x16;
And if you like the one linear bit packing
r_o <= {r_o[14:12], r_o[11]^r_o[15]^x16, r_o[10:5], r_o[4]^r_o[15]^x16,r_o[3:0], r_o[15]^x16}
crdsClear={{y=56,x=50,symbolName=3,},
{y=56,x=29,symbolName=2,},
{y=56,x=99,symbolName=2,},
{y=56,x=9,symbolName=5,},
{y=56,x=69,symbolName=5,},
{y=56,x=19,symbolName=4,},
{y=56,x=59,symbolName=4,},
{y=56,x=89,symbolName=4,},
{y=56,x=40,symbolName=7,},
{y=56,x=80,symbolName=6,},}
tmp2={}
ywf = 1
table.sort(crdsClear,
function(a,b)
tmp2[ywf]=""
for i=1, #crdsClear, 1 do tmp2[ywf] = tmp2[ywf].."\t"..crdsClear[i].x end
ywf = ywf + 1
if a.x <= b.x then print(a.x.." <= "..b.x.." true") else print(a.x.." <= "..b.x.." false") end
return a.x <= b.x -- a.y <= b.y and
end
)
-- Create string
order=""
print(#crdsClear)
result = {[1]=""}
for i=1, #crdsClear, 1 do
order = order..crdsClear[i].x.." "
result[1] = result[1].. crdsClear[i].symbolName
end
print(order)
print(result[1])
I have .x order after sorting:
9 19 59 29 40 50 69 80 89 99
and string:
5442735642
Why i have incorrect order?
If i change:
return a.x <= b.x
to:
return a.x < b.x
then order full correctly.
From the Lua reference manual:
If comp is given, then it must be a function that receives two list
elements and returns true when the first element must come before the
second in the final order (so that, after the sort, i < j implies not
comp(list[j],list[i])).
Using <= here results in an invalid sort function which in some cases will invoke an error message and/or an incomplete sort result.
Use return a.x < b.x instead.
The following is the code to convert a number to binary string. Can anyone tell me how ans.push_back((char)('0' + rem)) works?
class Solution {
public:
string findDigitsInBinary(int n) {
string ans;
if (n == 0) return "0";
while (n > 0) {
int rem = n % 2;
ans.push_back((char)('0' + rem));
n /= 2;
}
reverse(ans.begin(), ans.end());
return ans;
}
};
To understand it, you just need to know that you can do arithmetic operations on char variables too. So, the simple loop below is valid and will print 0123456789.
for(char c = '0'; c <= '9'; ++c)
cout << c;
In you code, rem is either 0 or 1. So, (char)('0'+rem) is either '0' or '1' as desired, corresponding to rem=0, 1, respectively.
while (n > 0) {
int rem = n % 2;
ans.push_back((char)('0' + rem));
n /= 2;
}
Focus on this loop. Suppose n is 5
n > 0 true so enter into loop. rem = n % 2 so rem = 5 % 2 = 1
ans.push_back((char)('0' + rem)) here ('0' + rem) is (48 + 1) ASCII of '0' is 48
Now convert 48 + 1 = 49 into char that is '1'. Now push '1' into ansand then n /= 2 is 5 /= 2 that is 2. Now go back and check the condition in while loop. After loop reverse the content of ans and you have binary string of number n
First you get rem as %2. Thus the value of rem can either be 0 or 1.
In ans.push_back((char)('0' + rem)); you need to add the corresponding character to the string, that is either 0 or 1. For this you have considered '0' as base character and you simply add the rem to it, using its ASCII int. When doing such integer operation, the ASCII value of character '0' is considered, which is 48. Thus after adding rem to it, it can either be 48 + 0 = 48 or 48 + 1 = 49.
Finally, this value is type casted back to char, with 48 being '0' and 49 being '1'
I want to see if a variable is between a range of values, for example if x is between 20 and 30 return true.
What's the quickest way to do this (with any C based language)?
It can obviously be done with a for loop:
function inRange(x, lowerbound, upperbound)
{
for(i = lowerbound; i < upperbound; i++)
{
if(x == i) return TRUE;
else return FALSE;
}
}
//in the program
if(inRange(x, 20, 30))
//do stuff
but it's awful tedious to do if(inRange(x, 20, 30)) is there simpler logic than this that doesn't use built in functions?
The expression you want is
20 <= x && x <= 30
EDIT:
Or simply put in in a function
function inRange(x, lowerbound, upperbound)
{
return lowerbound <= x && x <= upperbound;
}
Python has an in operator:
>>> r = range(20, 31)
>>> 19 in r
False
>>> 20 in r
True
>>> 30 in r
True
>>> 31 in r
False
Also in Python, and this is pretty cool -- comparison operators are chained! This is totally unlike C and Java. See http://en.wikipedia.org/wiki/Python_syntax_and_semantics#Comparison_operators
So you can write
low <= x <= high
In Python -10 <= -5 <= -1 is True, but in C it would be false. Try it. :)
Why not just x >= lowerbound && x <= upperbound ?