Specman: How to print negative hexadecimals? - specman

is it possible to print negative hexadecimals in Specman?
For example:
var foo : int;
foo = -0x5;
print foo;
will print: foo = 0xfffffffb.
How can I display the output as -0x5?
Really appreciate any help.

You can try a trick as follows:
if foo >= 0 then {
out(foo);
} else {
out("-", -foo);
};

After having a look in the doc, I don't think this is possible. Hex notation is usually used to figure out the bit representation so it doesn't really make sense to show a -. If you want to see it in decimal format (regardless of the radix settings), do:
print foo using dec;
or:
print dec(foo);

Related

Syntax error with UCASE$?

Not sure what this means. It says it's a syntax error with UCASE$ but can I not put letter$ in there?
CLS
PRINT "Do you want lower case or upper case? (U/L)"
DO
CASED$ = INKEY$
LOOP UNTIL CASED$ = "U" OR CASED$ = "L"
IF CASED$ = "L" THEN
FOR char = 1 TO 26
READ letter$
PRINT letter$; " = "; ASC(letter$)
SLEEP 1
NEXT char
ELSE
FOR char = 1 TO 26
READ letter$
UCASE$(letter$)
PRINT letter$; " = "; ASC(letter$)
SLEEP 1
NEXT char
END IF
DATA a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z
UCASE$ is a function that returns a string. You need to pass what it returns into a variable. A common behavior of most built-in functions in BASIC is that they can stand in place of a variable or expression.
letter$ = UCASE$(letter$)
To demonstrate why this is, try
a$ = "h"
PRINT a$, UCASE$(a$)
in a new program. As you can see, UCASE$ itself becomes the new string instead of manipulating the original string. If you want to preserve the result of the function, you must pass it into a variable.

Perl mantissa differ from other doubles

I'm trying to scan in a float: 13.8518009935297 .
The first routine is my own, the second is MacOSX libc's
strtod, the third is GMP's mpf_get_d() the forth is
perls numeric.c:Perl_my_atof2().
I use this snippet to print the mantissa:
union ieee_double {
struct {
uint32_t fracl;
uint32_t frach:20;
uint32_t exp:11;
uint32_t sign:1;
} s;
double d;
uint64_t l;
};
union ieee_double l0;
l0.d = ....
printf("... 0x%x 0x%x\n", l0.s.frach, l0.s.fracl);
The return values for the four functions are:
my-func : 0xbb41f 0x4283d21b
strtod : 0xbb41f 0x4283d21c
GMP : 0xbb41f 0x4283d21b
perl : 0xbb41f 0x4283d232
The difference between the first three functions is rounding.
However perl's mantissa is quite out of sync.
If I print all four doubles to a string again I get the
same decimal double back, the numbers seem to be equal.
My question:
The difference between my-func, strtod, GMP is rounding. However,
why is perl's mantissa so much out of sync, but still, if
converted back to decimal, it ends up as the same number again.
The difference is 22, so it should be noted in a decimal
fraction. How can I explain this?
Append:
Sorry, I think I figured out the problem:
$r = rand(25);
$t = $p->tokenize_str("$r");
tokenize_str() was my implementation of a conversion from string to double.
However the perl stringify "$r" prints out $r as 13.8518009935297, which is a
already truncation.
The actual value of $r is different, so when I at the end the binaries of
$t with $r I get values that diverge.
Here is some perl code to answer your question:
perl -le '($frac1, $frach)=unpack("II", pack "d", .0+"13.8518009935297");
print sprintf("%d %d 0x%03x 0x%04x", ($frach >> 31)&1, ($frach>>20)&0x5ff, $frach & 0xfffff, $frac1)'
-> 0 1026 0xbb41f 0x4283d21c
Perl gives the same result as strtod. The difference was the mistake you indicated in append.

How do you concatenate strings in a Puppet .pp file?

Here is my naive approach:
# puppet/init.pp
$x = 'hello ' +
'goodbye'
This does not work. How does one concatenate strings in Puppet?
Keyword variable interpolation:
$value = "${one}${two}"
Source: http://docs.puppetlabs.com/puppet/4.3/reference/lang_variables.html#interpolation
Note that although it might work without the curly braces, you should always use them.
I use the construct where I put the values into an array an then 'join' them.
In this example my input is an array and after those have been joined with the ':2181,' the resulting value is again put into an array that is joined with an empty string as separator.
$zookeeperservers = [ 'node1.example.com', 'node2.example.com', 'node3.example.com' ]
$mesosZK = join([ "zk://" , join($zookeeperservers,':2181,') ,":2181/mesos" ],'')
resulting value of $mesosZK
zk://node1.example.com:2181,node2.example.com:2181,node3.example.com:2181/mesos
Another option not mentioned in other answers is using Puppet's sprintf() function, which functions identically to the Ruby function behind it. An example:
$x = sprintf('hello user %s', 'CoolUser')
Verified to work perfectly with puppet. As mentioned by chutz, this approach can also help you concatenate the output of functions.
The following worked for me.
puppet apply -e ' $y = "Hello" $z = "world" $x = "$y $z" notify { "$x": } '
notice: Hello world
notice: /Stage[main]//Notify[Hello world]/message: defined 'message' as 'Hello world'
notice: Finished catalog run in 0.04 seconds
The following works as well:
$abc = "def"
file { "/tmp/$abc":
You could use the join() function from puppetlabs-stdlib. I was thinking there should be a string concat function there, but I don't see it. It'd be easy to write one.
As stated in docs, you can just use ${varname} interpolation. And that works with function calls as well:
$mesosZK = "zk://${join($zookeeperservers,':2181,')}:2181/mesos"
$x = "${dirname($file)}/anotherfile"
Could not use {} with function arguments though: got Syntax error at '}'.

Parse negative numbers from string in perl

How do I parse a negative number from a string in perl? I have this piece of code:
print 3 - int("-2");
It gives me 5, but I need to have 3. How do I do it?
Perl will automatically convert between strings and numbers as needed; no need for an int() operation unless you actually want to convert a floating point number (whether stored as a number or in a string) to an integer. So you can just do:
my $string = "-2";
print 3 - $string;
and get 5 (because 3 minus negative 2 is 5).
Well, 3 - (-2) really is 5. I'm not really sure what you want to achieve, but if you want to filter out negative values, why not do something like this:
$i = int("-2")
$i = ($i < 0 ? 0 : $i);
This will turn your negative values to 0 but lets the positive numbers pass.
It seems to be parsing it correctly.
3 - (-2) is 5.
If it was mistakenly parsing -2 as 2 then it would have output 3 - 2 = 1.
No matter how you add/subtract 2 from 3, you will never get 3.
You are probably thinking of some other function instead of 'int'.
try:
use List::Util qw 'max';
...
print 3 - max("-2", 0);
if you want to get 3 as result.
Regards
rbo

In Powershell how can I convert a string with a trailing 'sign' to a number?

I need to convert strings with optional trailing signs into actual numbers using Powershell.
Possible strings are:
1000-
323+
456
I'm trying to use System.Int.TryParse with a NumberStyles of AllowTrailingSign, but I can't work out how to make System.Globalization.NumberStyles available to Powershell.
EDIT: as per Halr9000's suggestion
$foo = "300-";
$bar = 0;
$numberStyles = [System.Globalization.NumberStyles];
$cultureInfo = [System.Globalization.CultureInfo];
[int]::TryParse($foo, $numberStyles::AllowTrailingSign, $cultureInfo::CurrentCulture, [ref]$bar);
[System.Globalization.NumberStyles]::AllowTrailingSign
I should also point out, that when I'm dealing with enums in general, sometimes I can get by typing a string. E.g. in this case, just put
"AllowTrailingSign"
Final note, when quizzing an Enum for all possible values, use the line:
[System.Globalization.NumberStyles] | gm -static
Here's a better way to get the enum values:
$type = [System.Globalization.NumberStyles]
[enum]::GetValues($type)
If you are sure that the signs could be - or +, String.Replace could help.
If you mean that 323- should return -323, checking for the sign and multiplying it by -1 would help.