How to compare negative numbers (2's complement) in verilog? - comparison

I have the following comparison statement in verilog, which works fine (positive comparison)
if((count_cc >= 13'b0000000011110)&&(count_cc <= 13'b0000000111100)) //30,60
begin
level=4'b0010; //level 2
end
However, when I use 2's complement, it does not work,
if((count_cc >= 13'b1111111100100)&&(count_cc <= 13'b1111111110110)) //-10 , -28
begin
level=4'b0101; //level 5
end
Any guidance would be helpful.

Assuming count_cc is already declared signed, use
if((count_cc >= 13'sb1111111100100)&&(count_cc <= 13'sb1111111110110)) //-10 , -28
begin
level=4'b0101; //level 5
end
of course, this would also work
if((count_cc >= -10)&&(count_cc <= -28)

Related

Pine language for tradinview, error with variablie 'i'

I have this code in Pine script for tradingview:
if close >= base_bar_low and close <= base_bar_high
//Check for one or more neighboring bars within 50-50 range of Stochastic or Williams %R
i := 0
for i in range(2, stoch_length + 1):
if (stoch_D[i] <= 50) and (stoch_D[i] >= 50) or (willR[i] >= -50) and (willR[i] <= 50):
//Check for last bar close within 30% of base bar's high
last_bar_high = high[1] + (atr * 0.3)
if close <= last_bar_high:
plot("W1-Long")
end
and I have this error: 'i' is not a valid type keyword in variable declaration - how can I avoid it?

Is there a way to perform if-else using collect in ruby and update an array

$days = 25
action_count = [0,0,0]
if $days < 0
action_count[0] += 1
elsif $days <= 20
action_count[1] += 1
else
action_count[2] += 1
end
Can this if-else code be shortened using collect in ruby
I can't think of a way to do this using collect, but you could use Enumerable#bsearch_index.
If $days is an integer, this does the same thing as your if; but it is not very readable. (Integer, because there is a slight problem that you are using two different comparisons, and I'm assuming $days <= 20 is the same as $days < 21).
action_count[[0, 21].bsearch_index { |x| $days < x } || -1] += 1
bsearch_index finds whether $days is lower than 0, 21 or neither, returning 0, 1 or nil. We replace the nil case with -1 (last element), and we have an index we can use to increment an appropriate element of action_count.

Converting a Weka classifier into a score

I'm trying to convert my classifier result from classifying instances as 0 or 1, to instead give a score (confidence measure?), say between 0 and 10,
I am using a RIDOR classifier but could also use ClassificationViaRegression, RandomForest or AttributeSelectedClassifier just as easily, although they don't classify quite as well.
I have output everything I can to the terminal (all the options checked), but I can't find a confidence measure anywhere in the predictions. In addition I understand none of these have the option to output the source code? In which case i'll have to code the classifiers manually.
Here is an example of the rules generated:
class = 2 (40536.0/20268.0)
Except (fog <= 14.115114) and (polySyllabicWords/Sentence <= 1.973684) and (polySyllabicWords/Sentence <= 1.245) and (Characters/Word > 4.331715) => class = 1 (2309.0/5.0) [1137.0/4.0]
Except (fog <= 14.115598) and (polySyllabicWords/Sentence <= 1.973684) and (polySyllabicWords/Sentence > 1.514706) => class = 1 (2281.0/0.0) [1112.0/0.0]
Except (fog <= 14.136126) and (Words/Sentence > 19.651515) and (polySyllableCount <= 10.5) and (polySyllabicWords/Sentence > 2.416667) and (Syllables/Sentence <= 34.875) => class = 1 (601.0/0.0) [303.0/6.0]
Except (fog <= 14.140863) and (polySyllabicWords/Sentence <= 1.944444) and (polySyllableCount <= 4.5) and (polySyllabicWords/Sentence <= 1.416667) and (wordCount > 29.5) and (Characters/Word <= 4.83156) => class = 1 (333.0/0.0) [152.0/0.0]
Except (fog <= 14.142217) and (polySyllabicWords/Sentence <= 1.944444) and (polySyllableCount <= 4.5) and (polySyllabicWords/Sentence <= 1.416667) and (numOfChars > 30.5) and (Syllables/Word <= 1.474937) => class = 1 (322.0/0.0) [174.0/4.0]
Except (fog <= 14.140863) and (polySyllabicWords/Sentence <= 1.75) and (polySyllableCount <= 4.5) => class = 1 (580.0/28.0) [298.0/21.0]
Except (fog <= 14.141508) and (Syllables/Sentence > 25.585714) and (Words/Sentence > 19.683333) and (sentenceCount <= 4.5) and (polySyllabicWords/Sentence <= 2.291667) and (fog > 12.269468) => class = 1 (434.0/0.0) [202.0/0.0]
Except (fog <= 14.140863) and (Syllables/Sentence > 25.866071) and (polySyllableCount <= 16.5) and (fog > 12.793102) and (polySyllabicWords/Sentence <= 2.9) and (wordCount <= 59.5) and (Words/Sentence > 16.166667) and (Words/Sentence <= 24.75) => class = 1 (291.0/0.0) [166.0/0.0]
Except (fog <= 14.140863) and (Syllables/Sentence > 25.585714) and (Words/Sentence > 19.630682) and (polySyllabicWords/Sentence > 2.656863) and (polySyllableCount <= 16.5) and (fog > 13.560337) and (Words/Sentence <= 21.55) and (numOfChars <= 523) => class = 1 (209.0/0.0) [93.0/2.0]
Except (fog <= 14.147578) and (Syllables/Word <= 1.649029) and (polySyllabicWords/Sentence <= 1.75) and (polySyllabicWords/Sentence > 1.303846) and (polySyllabicWords/Sentence <= 1.422619) and (fog > 9.327132) => class = 1 (183.0/0.0) [64.0/0.0]......
I am also unsure what the first line means (40536/20368) - does that just mean classify it as 2, unless one of the following rules apply?
Any help is much appreciated!
Generally, deriving confidence from classifiers is not regarded as an easy task, especially if you'd like it calibrated (e.g. presented as chances of the classification being correct). However, there are several relatively easy ways of getting rough estimations.
With tree and rule based classifiers, the numbers in parentheses represent the number of correct/incorrect samples included in the bucket. So, for instance, a bucket with (20,2) would mean there were 20 cases where this rule was correct, and 2 where it was incorrect, based on the train data. You could use this ratio as a rough measure of confidence.
When using regression, you can get WEKA to output the actual numeric result of the classifier (rather than just the class), and base a measure of confidence on it.
More generally, following the documentation, you can use the -p option of the commend line (see here). However, I'm not certain how these numbers are calculated.

Generate random number in a float range

How we can generate randomize number between a range in the Float numbers (in delphi xe3) ?
For example, randomize number between [0.10 to 0.90].
I need give results like:
[ 0.20 , 0.32 , 0.10 , 0.50 ]
Thanks for solutions....
Another option is to use RandomRange (returns: AFrom <= r < ATo) as follow:
RandomRange(10, 90 + 1) / 100
or
RandomRange(10, 90 + 1) * 0.01
will return numbers in the range of 0.10 to 0.90 (including 0.90)
var
float : Double;
float := Random; // Random float in range: 0 <= float < 1
float := 0.1 + float*0.8 // 0.1 <= float < 0.9
To initialize the Random number generator, make a single call to Randomizeor set the RandSeed parameter before calling the Random function for the first time.
Not doing so, generates the same sequence every time you run the program. Note however, that this sequence is not guaranteed when recompiling for another compiler version.
Try this:
function RandomRangeF(min, max: single): single;
begin
result := min + Random * (max - min);
end;
This is a bit cheeky but here goes: Depends how many numbers you want after the floating point. For example, if you want 1 number, you could generate in the 100 - 999 range and then divide by 10. Or 1000 - 9999 and divide by 100.

Lua: Random: Percentage

I'm creating a game and currently have to deal with some math.randomness.
As I'm not that strong in Lua, how do you think
Can you make an algorithm that uses math.random with a given percentage?
I mean a function like this:
function randomChance( chance )
-- Magic happens here
-- Return either 0 or 1 based on the results of math.random
end
randomChance( 50 ) -- Like a 50-50 chance of "winning", should result in something like math.random( 1, 2 ) == 1 (?)
randomChance(20) -- 20% chance to result in a 1
randomChance(0) -- Result always is 0
However I have no clue how to go on, and I completely suck at algorithms
I hope you understood my bad explanation of what I'm trying to accomplish
With no arguments, the math.random function returns a number in the range [0,1).
Lua 5.1.4 Copyright (C) 1994-2008 Lua.org, PUC-Rio
> =math.random()
0.13153778814317
> =math.random()
0.75560532219503
So simply convert your "chance" to a number between 0 and 1: i.e.,
> function maybe(x) if math.random() < x then print("yes") else print("no") end end
> maybe(0.5)
yes
> maybe(0.5)
no
Or multiply the result of random by 100, to compare against an int in the range 0-100:
> function maybe(x) if 100 * math.random() < x then print(1) else print(0) end end
> maybe(50)
0
> maybe(10)
0
> maybe(99)
1
Yet another alternative is to pass the upper and lower limits to math.random:
> function maybe(x) if math.random(0,100) < x then print(1) else print(0) end end
> maybe(0)
0
> maybe(100)
1
I wouldn't mess around with floating-point numbers here; I'd use math.random with an integer argument and integer results. If you pick 100 numbers in the range 1 to 100 you should get the percentages you want:
function randomChange (percent) -- returns true a given percentage of calls
assert(percent >= 0 and percent <= 100) -- sanity check
return percent >= math.random(1, 100) -- 1 succeeds 1%, 50 succeeds 50%,
-- 100 always succeeds, 0 always fails
end

Resources