I need to convert floats into numbers that resemble measurements from a ruler. For example: 3.75 needs to be converted into 3 and 3/4. However, this is harder than it would seem at first, because I need to keep the denominator in a form that is easily translated into a ruler measurement by a human. Essentially, the denominator should only be powers of 2, up to 16. I don't want a fraction like 3/5 because 5'ths aren't marked on a ruler. I have figured out how to limit the denominator from going above 16, but I can't figure out how to keep the denominator a power of 2.
Answers in python or c++ is preferred.
extract integer part, so you have fraction part less than 1.
find nearest 16th of fraction: multiply by 16 and round to nearest integer. Have some policy to break ties (e.g. round to even). I believe this step can't introduce floating point arithmetic error because you are multiplying by a power of 2.
reduce n/16 to lowest terms (cancel out common multiples of 2). I guess you need to compute the greatest common divisor. In Python that's fractions.gcd, dunno about C++.
I did what Jhecht said because it seemed easy to do with python dictionary.
Related
We have some weird calculation scenario in Microsoft excel, this simple addition operation resulting 1 at 13 decimal digit whereby it should be zero
But when i extract the value in formula the result is correct (both formula value is the same but the result is different)
From human understanding addition calculation will reduce the decimal digit rather than add it.
Is this by design or bugs ?
I strongly guess that this is a gap from the limited precision of floating point numbers. Accuracy of digital numbers is limited. Numbers in excel are saved in binary format(but displayed in decimal format). This means that the "0" is not as protected as in the decimal system. Usually Excel tries to cover this up for examples like yours.
Also, if your numbers derive from complex calculations (e.g. square-roots), the accuracy can be limited as most functions use approximation with limited iterations to give a result.
You can find more information about floating point arithmetic here. The blog is about pythin but the way it works is similar.
https://docs.python.org/3/tutorial/floatingpoint.html
I have a value of €1850.50. I want a calculation to round this to €1851
My formula is
ToNumber(Round(Replace([Item Data.LHC Part Anaes Rate 2019 (No Rounding)],"€","")))
Currently, it is bringing back €1850. It seems to round values of .5 downwards instead of upwards? This issue is only happening when the value is .50
The reason you're seeing this odd behavior is because .NET (the framework Blue Prism is based on) uses Banker's Rounding to perform rounding functions by default. From the linked page:
Bankers Rounding is an algorithm for rounding quantities to integers, in which numbers which are equidistant from the two nearest integers are rounded to the nearest even integer. Thus, 0.5 rounds down to 0; 1.5 rounds up to 2.
Thus, when leveraging the rounding functionality in a typical calculation stage, 0.5 will round to 0.
To counter this implementation of rounding, you can use one of two methods:
Method #1 - Processing of 0.5 as a separate case
Use decision stages to determine whether or not the number you're attempting to round has five tenths at the end. If yes, add another .5 to "round up". If there's any other decimal number, proceed with the rounding as normal.
Method #2 - Custom rounding implementation
Create a new custom object with an action that takes your number as an input. Write a code stage to implement the rounding as you see fit. This SO question body has some good code you can start with.
Why not use the RndUp function instead? Although I would expect .5 to be rounded up as well.
Also, the order of the functions in your code is not correct, you are first rounding and then converting to number. The Round function performs (or tries to) do the conversion automatically.
It could look like this:
RndUp(ToNumber(Replace("€1850.50","€","")))
Please anyone can share your opinion excel how to calculate the PI() value.
In Execl while Calculating =TAN(30*PI()) formula it returns -1.07809E-14.If i directly give the PI() Value(3.141593) it returns 1.03923E-05.
It's almost certainly not calculated at all, but simply stored as a constant at the maximum significance for the machine.
That's pretty certainly more than six decimal places.
Here's the first 20 digits, try putting that in: 3.14159265358979323846
The accuracy or otherwise of Excel's value for π is perhaps less of the issue here than that of Excel's TAN function. TAN takes an argument in radians and 30*PI() is the same angle as 2*PI(), ie 360 degrees (a complete circle - though it might be argued that 30*PI() is a circle after 15 complete turns). The tangent of 360o is 0 (or at least the tangent tends to 0 as the angle approaches 360o) hence whatever the accuracy chosen for π, any answer other than 0 is incorrect.
-1.07809E-14 is nearer 0 than 1.03923E-05 because as has been pointed out, Excel is normally accurate to at least 14 digits and PI() as 3.14159265358979 is more accurate than pi as 3.141593. However -0.0000000000000107809 is nevertheless inaccurate at the 14th decimal place.
A more sensible comparison may be between the tangent of 30o with PI() and 3.141593:
=TAN(30*PI()/180) = 0.57735026918963
=TAN(30*3.141593/180) = 0.57735034616967
To 7 DP the answers are the same.
Excel evaluates 30 times PI() as 94.2477796076938 whereas 30*3.14159265358979 as 94.2477796076937.
Microsoft lists the following:
MS Excel PI function
PI function
Description
Returns the number 3.14159265358979, the mathematical constant pi, accurate to 15 digits.
you can see this by typing:
=pi()
then increasing the decimal places until you see zeros eg:
3.14159265358979000000
A friend of mine discovered a really weird thing in MS Excel. Excel rounds down some specific numbers the wrong way, actually it rounds down a number that shouldn't need rounding.
As far as I have tested, it happens in most versions of MS Excel 2007+
Eg. the number 10358.165790 will be rounded down to 10358.1657899999.
Apparently it only happens in this interval: 8192.165790 - 65535.165790.
It is really weird - it doesn't happen with eg. .165890 or .165690, only with .165790.
Do any of you know why this happens and why it only accounts to certain numbers?
Excel uses an IEEE754 64 bit double precision floating point type to represent numeric data; with some clever formatting and roundup tricks to get sums like 1/3 + 1/3 + 1/3 correct.
What you are observing is a natural consequence of that numeric scheme only being accurate to 15 significant figures. Unless the number happens to be a dyadic rational, in which case it can be stored exactly, the closest representable number is chosen to the one you actually want. This may be below or above a rounding cutoff.
It will occur in other ranges other than the one you cite too.
I have a database which houses scaled integers, the longest being 10 digits long. I am attempting to convert these to decimal values in Excel, moving the decimal point left by 4 digits, i.e. dividing by 10000.
Given that these integers are currently under the 15-digit significant figure limit, and will remain so, is there a possibility that I can encounter rounding errors?
is there a possibility that I can encounter rounding errors?
Strictly speaking I think yes. For example:
but what may be significant is that the discrepancy as shown (all formatted the same, the smaller black ones created by formula, the red ones by difference of those immediately above) is in the tenth decimal place, so hopefully not a problem.