Is there something like printf in Action Script 3? - actionscript-3

I searched it for while but didn't find anything like printf in Action Script.
That makes it a little difficult to generate formated strings.

Printf-as is a third-party library that will handle this. Here is the GitHub repo. From the README:
printf("You can also display numbers like PI: %f, and format them to a fixed precision,
such as PI with 3 decimal places %.3f", Math.PI, Math.PI);
// outputs: " You can also display numbers like PI: 3.141592653589793,
// and format them to a fixed precision, such as PI with 3 decimal places 3.142"
It also plays well with dates:
var date : Date = new Date();
printf("Today is %d/%m/%Y", date, date, date);

think you might be looking for StringUtil.substitute()
take a look here:
http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/mx/utils/StringUtil.html#substitute

There is the Formatter classes that you could use or create custom formatters.
http://livedocs.adobe.com/flex/3/html/help.html?content=createformatters_2.html

The only function I know that prints to a standard output is trace() (debug purposes only) , which can take virtually any type of variable.
I might not have understood your question, though.

Related

JMETER - trying to test money values using beanshell

I am trying to execute tests using beanshell assertion. I have a csv file with Expected money amounts which are all to 2 decimal places eg 145.16, 1945.21 etc and i wish to compare them to actual values that will come back from my sampler http response with the same format. I wish my test case to pass if the difference between the two is < 0.1 i.e. 10 cents/pence etc.
I started by parsing the initial string values to doubles or floats or shorts and using Math.abs to compare but of course the accuracy was not there eg if the difference was actually 10 cents(FAIL) the calculation was actually be say 0.999999765 or similar and so the test case would incorrectly PASS.
I have now moved onto BigDecimal with little success. I have tried to use setScale which has made the comparsion a bit more accurate.
So my question is BigDecimal the way to go? What do i do with the BigDecimal after i have created it - if i convert it to a short or float etc i get the same problem again. Would DecimalFormat help ? I need the values to be with two decimal points at the point where i use Math.abs - is there an alternative to Math.abs ?
Hope that makes sense and thanks in advance.
BigDecimal is quite good candidate to go with, at least all Java solutions which are designed to work with money objects are using BigDecimal under the hood. You may find ROUND_HALF_EVEN property useful
Be aware that there is Joda-Money library which provides Money.isGreaterThan() method which can be used instead of custom logic.
Starting from JMeter 3.1 it is recommended to use JSR223 Test Elements and Groovy language for any form of scripting so consider switching to Groovy as soon as it will be possible.
FYI this worked for me in the end -
import java.math.BigDecimal;
BigDecimal Actual_PAF = new BigDecimal("${Actual_PAF}");
BigDecimal Expected_PAF = new BigDecimal("${Expected_PAF}");
BigDecimal ActualPAFDifference = new
BigDecimal(Actual_PAF.subtract(Expected_PAF).toString());
if (ActualPAFDifference.abs() < 0.001)
{ Failure=false;
vars.put("PAFPassOrFail","PASS");}
else
{ Failure=true;
vars.put("PAFPassOrFail","FAIL");
}

how do i decode/encode the url parameters for the new google maps?

Im trying to figure out how to extract the lat/long of the start/end in a google maps directions link that looks like this:
https://www.google.com/maps/preview#!data=!1m4!1m3!1d189334!2d-96.03687!3d36.1250439!4m21!3m20!1m4!3m2!3d36.0748342!4d-95.8040972!6e2!1m5!1s1331-1399+E+14th+St%2C+Tulsa%2C+OK+74120!2s0x87b6ec9a1679f9e5%3A0x6e70df70feebbb5e!3m2!3d36.1424613!4d-95.9736986!3m8!1m3!1d189334!2d-96.03687!3d36.1250439!3m2!1i1366!2i705!4f13.1&fid=0
Im guessing the "!" is a separator between variables followed by XY where x is a number and y is a lower case letter, but can not quite figure out how to reliably extract the coordinates as the number/order of variables changes as well as their XY prefixes.
ideas?
thanks
Well, this is old, but hey. I've been working on this a bit myself, so here's what I've figured out:
The data is an encoded javascript array, so the trick when trying to generate your own data string is to ensure that your formatting keeps the structure of the array intact. To do this, let's look at what each step represents.
As you're correctly figured out, each exclamation point defines the start of a value definition. The first character, an int value, is an inner count, and (I believe) acts as an identifier, although I'm not 100% certain on this. It seems to be pretty flexible in terms of what you can have here, as long as it's an int. The second character, however, is much more important. It defines the data type of the value. I don't know if I've found all the data types yet, but the ones I have figured out are:
m: matrix
f: float
d: double
i: integer
b: boolean
e: enum (as integer)
s: string
u: unsigned int
x: hexdecimal value?
the remaining characters actually hold the value itself, so a string will just hold the string, a boolean will be '1' or '0', and so on. However, there's an important gotcha: the matrix data type.
The value of the matrix will be an integer. This is the length of the matrix, measured in the number of values. That is, for a matrix !1mx, the next x value definitions will belong to the matrix. This includes nested matrix definitions, so a matrix of form [[1,2]] would look like !1m3!1m2!1i1!2i2 (outer matrix has three children, inner matrix has 2). this also means that, in order to remove a value from the list, you must also check it for matrix ancestors and, if they exist, update their values to reflect the now missing member.
The x data type is another anomaly. I'm going to guess it's hexdecimal encoded for most purposes, but in my particular situation (making a call for attribution info), they appear to also use the x data type to store lat/long information, and this is NOT encoded in hex, but is an unsigned long with the value set as
value = coordinate<0 ? (430+coordinate)*1e7 : coordinate*1e7
An example (pulled directly from google maps) of the x data type being used in this way:
https://www.google.com/maps/vt?pb=!1m8!4m7!2u7!5m2!1x405712614!2x3250870890!6m2!1x485303036!2x3461808386!2m1!1e0!2m20!1e2!2spsm!4m2!1sgid!2sznfCVopRY49wPV6IT72Cvw!4m2!1ssp!2s1!8m11!13m9!2sa!15b1!18m5!2b1!3b0!4b1!5b0!6b0!19b1!19u12!3m1!5e1105!4e5!18m1!1b1
For the context of the question asked, it's important to note that there are no reliable identifiers in the structure. Google reads the values in a specific order, so always keep in mind when building your own encoded data that order matters; you'll need to do some research/testing to determine that order. As for reading, your best hope is to rebuild the matrix structure, then scan it for something that looks like lat/long values (i.e. a matrix containing exactly two children of type double (or x?))
Looks like the developer tools from current browsers (I am using Chrome for that) can give you a lot of info.
Try the following:
Go to Google Maps with Chrome (or adapt the instructions for other browser);
Open Developer Tools (Ctrl + Shift + I);
Go to Network tab. Clear the current displayed values;
Drag the map until some url with encoded data appears;
Click on that url, and then go to the Preview sub-tab;
Try this.
function URLtoLatLng(url) {
this.lat = url.replace(/^.+!3d(.+)!4d.+$/, '$1');
this.lng = url.replace(/^.+!4d(.+)!6e.+$/, '$1');
return this;
}
var url = new URLtoLatLng('https://www.google.com/maps/preview#!data=!1m4!1m3!1d189334!2d-96.03687!3d36.1250439!4m21!3m20!1m4!3m2!3d36.0748342!4d-95.8040972!6e2!1m5!1s1331-1399+E+14th+St%2C+Tulsa%2C+OK+74120!2s0x87b6ec9a1679f9e5%3A0x6e70df70feebbb5e!3m2!3d36.1424613!4d-95.9736986!3m8!1m3!1d189334!2d-96.03687!3d36.1250439!3m2!1i1366!2i705!4f13.1&fid=0');
console.log(url.lat + ' ' + url.lng);

How to read in a user defined math function

I'm trying to build a program in Pascal to differentiate mathematical functions. It's working very well (calculate min/max, symmetry, drawing the graph, etc.) but I have to put the functions (i.e. x^3+3x+2) into the source code like this:
function f(x : real): real;
begin
f := x * x * x + 3 * x + 2;
end;
Though, I want the user to define the function to differentiate. Obviosly the readln function does not help.
Somebody told me the only solution would be a specific parser. But it's very difficult, and I don't know how to do it.
My idea would be to extract the function into a *.txt file for example so that it could be changed easily. Is that possible?
Can somebody show me a parser which could solve this problem or have anybody some other great solution?
I would really appreciate your help!
Thanks in advance ;)
Free Pascal ships with the symbolic package, which has both a parser and evaluator for mathematical expressions. You can probably use this as a starting point. See the documentation for usage.
There are also a number of parsers/evalutaors on SWAG:
EQUATE.PAS (short, clean evaluator)
PARSMATH.PAS (very short example code)
Math Parsing Unit (undocumented, kind of messy)
Math Evaluations (Somewhat cryptic.)
Nice Expression Parser (Small, seems well done.)
Expression Evaluator (Messier, includes trig functions.)
Math Expression Evaluator (not well documented)
Equation Parser (converts equations to arrays of coefficients)
Text Formula Parser (fairly complete parser/evaluator unit)
I bolded the ones I thought were the most useful. I don't think any of them are as complete as the symbolic package in my other answer, but they might be worth reading if you need help.
(All of this is fairly old code. Unless otherwise stated, the rule with SWAG is to treat this stuff as having a new-style BSD license)

As3 BigInteger returns an Incorrect Answer

I am trying to implement a RSA encryption program in flash. I looked into working with Big Numbers and found the BigInteger var type in the Crypto package. I started playing around with BigIntegers but my outputs are never the correct answer. For example the below code will output 5911 when the answer should be 9409. Any input about this error would be great.
var temp:BigInteger = new BigInteger(String(97));
temp = temp.pow(2);
trace(temp.toString());
Output = 5911
I'm not sure which crypto package you are referring to, I though it was as3crypto but I don't remember it's implementions having a pow method that has that signature. But either way, you always have to remember what base you are dealing with and what the library was designed for.
(9716)2 = 591116
You are dealing with hex, not decimal, numbers.
Think of that geek-is-chic tshirt that says "There are 10 kinds of people. Those that understand binary and those that don't". In that case "10" is assumed to be 102. Which equals 210. Unqualified bases almost always ruin everybodys day.

Where do I get "junk" data to help test my code?

For my C class I've written a simple statistics program -- it calculates max, min, mean, etc. Anyway, I've gotten the program successfully compiled, so all I need to do now is actually test it; the only problem is that I don't have anything to test with.
In my case, I need a list of doubles -- my program needs to accept between 2 and 1,000,000; Is there some resource online that can produce lists of otherwise meaningless data? I know Lorem Ipsum gets used for typesetting, and I'm wondering if there's something similar for various types of numerical data.
Or am I out of luck, and I'll have to just create my own junk data?
The problem with testing software is not the source of the data, but the test set. I mean, can you test an int sum(int a, int b) method by just inputting random numbers to it? No, you need to know what to expect. This is a test set: inputs and expected outputs.
What do you say when you discover that 548888876+99814465=643503341? How can you tell this is the real result?
More than finding random numbers to give your program, you must somehow know the results of your computation in advance in order to compare it.
There are a few ways to do it: what I suggest you is to pick a random number generator (amphetamachine +1) and use the data both on your code and on a program that you already know is good, ie. Matlab for your purposes. After computing your statistics with both, compare your results and see if you coded good or need to do some debug.
By the way, I volountarily altered the result of the above sum...
What about just generating a random double?
Random r = new Random();
for (int i = 0; i < 100000; i++)
{
double number = r.NextDouble();
//do something with the value
}
Since the data you need will depend on the program, there is no source of generic data that I know of.
If you are able to write that program, you should be able to write a script to generate dummy data for yourself.
Just use a loop to print out random numbers within the range your program can accept.
Generate a file with random bytes:
$ dd \
of=random-bytes \
if=/dev/urandom \
bs=1024 \
count=1024
http://www.generatedata.com/#generator
I've used that data generator before with some success. To be fair, it will usually involve copy/pasting the data it generates into some other format that you'll be able to read in.
You can generate your own data for this specific case quite easily though. Loop a random number of times with a terminating condition of 1,000,000. Generating random doubles within the range you expect. Feed that in and away you go.
Generating your own test data in this case is probably the best option.
You could take the first million digits of pi and chop them up into however many doubles you want.
The first few could be 3.14159, 2.65358, 9.79323, 8.46264, 3.38327, 9.50288, 4.19716, and 9.39937, for example.