multiplying and rounding numbers using sed - sed

I have the following question for the scripting experts: how to multiply the number i.e. 12.34567 by 100 and then round it to two decimals using sed?
Thanks in advance!
Irek

Here's the source for an RPN calculator written in sed. That should get you started.

Related

Legacy code - looking for a function which emulates qbasic decimal to text

I could sit down a write this, but in the interests of not reinventing the wheel, I wanted to check that someone hasn't already done this.
I have to migrate over a little legacy tool which generates a text file containing a table of numeric values generated by a small tool. It was written many years back in DOS QBasic.
The only problem with the task is that QBasic had quite a few pecularities in decimal to text conversion. Lots of small exceptions.
The resultant file is imported into an old machine which works perfectly with the QBasic generated file, but when I pass it 6 or 7 digit precision decimals the results are not correct. QBasic output when writing decimals varies from 7 down to 3 digits of decimal depending on the whole number part and also generates decimals in the 0.0000E+1 format if there is no whole number part and there are zeros after the decimal point.
Has anyone seen a collection of functions which behave the same way as qbasic? Language doesn't matter. Googling hasn't turned up anything so far.

How to calculate check digits for a Hexadecimal IMEI (Number+Character) using luhn's algorithm?

I want to understand the logic so that I can implement this algorithm in java.
I want to calculate check digit for a valid hexadecimal IMEI number.
For example - 6C4BFFC0000004
Please help me with the algorithm.
I tried to find solution in google but I could not find correct answer using those algorithm.
Calculation what I did is like -
But the check digit for the above IMEI is 4. I am getting 7. I dont know where I am going wrong.
Actually, you do not need to convert to Decimal first. If you have an "IMEI" in hex it is really an MEID. IMEIs are a decimal-only subset of MEIDs. There is actually a patent out on how to calculate the Luhn for hex-based MEIDs. See claims 0113 through 0119 of the following:
https://patentimages.storage.googleapis.com/74/63/03/3fb507952c7ccf/US20080194223A1.pdf
I have had the same need and I found the solution by using the logic explained on the following website :
Luhn test of credit card numbers
I write a function based on the C example in order to determine the last digit. Behavior will be the same in Java.

Matlab: Obtaining an exact decimal number from a csv file

I have a CSV file where I have decimal numbers like 1.1, 1.10, 1.100. When I load the file in Matlab using importdata or even textscan, it show all these numbers same: 1.1 discarding 0's at the end. But, I have different meaning of them.
Is there anyway to recover?
Thanks,
Sam
You're saying you mean something different by 1.1 than by 1.100000? Mathematically, they are the same number (I sincerely hope you know this already).
So if your "numbers" don't have the same meaning as numbers in any of the strictly defined mathematical number systems (which MATLAB normally assumes is the case), you should import them as strings (%s) rather than numbers (%d, %f, etc.), and process them as such.

Rounding Pi in Perl to the 100 millionth decimal place?

For a Science Fair project, I am testing how your choice of programming language could affect performance. I am doing this by making scripts in Java, Ruby, Perl, and Python to calculate Pi to the 100 millionth decimal place. I'm starting with Perl, since I'm most familiar with Perl. However, this brings an interesting problem to the table. I need to round Pi to the 100 millionth digit in Perl, but as far as I can see, Perl has no good rounding method for this situation. There's only stuff like
use Math::Round;
$rounded = nearest(0.1, $numb);
And that's a bit of a problem, since I don't want to sit at my computer typing 100 million zeros. As far as I know, sprintf and printf aren't any better; plus, they have that annoying half to even thing. Can anyone help out?
P.S. I'm planning to use the Chudnovsky Formula, if it matters to anyone.
I don't think any programming language can natively do what you are asking. Even bignum libraries like Math::BigRat (default 40 digits) and Math::Bignum cannot do 100 million digits.
To make it happen, you will have to create your own custom way to represent such big numbers and how to round them.
Think about the problem in another way. You need to round to 100 million (1E8) digits but you don't need to process all 1E8 digits in one go to do that.
Instead,
Use the Chudnovsky Formula to calculate 1E8 +1 digits.
Store the digits in a string (if you have the memory) or a file.
Select the last n (something small like 8 or even 2) digits.
If they aren't all 9 round to n-1 digits.
If they are then convert them to (n-1) * 0 digits. Then read the next n digits from the end and repeat 4 and 5.
However, if the goal is to test relative performance of languages by generating 1E8 digits of Pi then why bother focus on the rather artificial constraint of rounding that number. If you use the same algorithm then any language should produce the same result. And you have a 50% chance of generating a rounded number anyway.
This is one step closer (though I haven't tested whether it can handle 100 million zeros). You'll need to use bignum to handle those sorts of numbers.
use bignum;
use Math::Round;
$rounded = nearest(1e-100_000_001, $numb);
Also, bignum has its own pi function with an accuracy parameter:
$rounded = bignum::bpi(100_000_001);

calculating large number powers in matlab

I am trying to figure out how to compute large powers of huge numbers in matlab to do
RSA encryption.
For example: A 50+ digit integer raised to the power of 999999.
You can use exponentiation by squaring:
https://en.wikipedia.org/wiki/Exponentiation_by_squaring
So the end result will be around 1e49^1e6 = 1e49000000. This is too large a number for any basic matlab datatype to hold. A solution is to use the vpi toolbox of the file exchange; it can handle large numbers, at the cost of speed.
A better solution would exist in getting your end objective on a different manner; ie redefine the formulas to get the final result..
We need to implement some form of large number data type
For C this is done using GMP Multiprecision library or LibToMMath Library
There are many others as well
For Matlab may be this will be helpful
>>> LInK <<<