Limit Decimal Places In Generated Numbers in R.Studio - decimal

I am a beginner in programmin in general and R specifically.
I would like to generate a set of random numbers in a normal distribution but to limit the decimal places in these numbers to only 2.
I have been using x1 <- runif() to generate my numbers.
Can I add something to it to enable me to only get results rounded off to 2 decimal places?

You can limit the decimal places using the round() function.
If I understand your question correctly this should do the trick:
x1 <- round(
runif(5, min=0, max=1)
, digits = 2
)
x1
The results, which will be different each time, are:
[1] 0.55 0.55 0.75 0.85 0.13

Related

Excel interpolate with hlookup

I have a table with x,y values. I want to interpolate for a given x1 value between the y values using the hllokup function also. I have found fomrulas for vlookup and xlookup but not for hlookup. I cannot use xlookup becaus eogf the verison of excel I use.
Example:
x-values 0.2 0.5 0.8 1.0 1.25 1.5 1.75 2.0 2.5 3.0 4.0
y-values 0.1 0.11 0.12 0.15 0.18 0.2 0.23 0.24 0.28 0.31 0.32
I need the y-value for x=1.1
I appreciate any help
There are various ways to interpolate: spline, polynomial, linear and so on.
I assume that you want linear interpolation between 2 x values.
In this case first of all, you need to find closest larger and closest lower x values:
Lower x:
=MAX(IF(B1:L1<B5,B1:L1))
Larger x:
=MIN(IF(B1:L1>B5,B1:L1))
Now need to find corresponding y's with HLOOKUP.
Lower x's y:
=HLOOKUP(A9,B1:L2,2,FALSE)
Larger x's y:
=HLOOKUP(B9,B1:L2,2,FALSE)
Now that you have all needed values you can write linear interpolation formula or you can use excel formula FORECAST. With 2 x's and 2 y's it will work as linear interpolation.
=FORECAST(B5,A11:B11,A9:B9)
Formula without using helper cells:
=FORECAST(B5,CHOOSE({1,2},HLOOKUP(MAX(IF(B1:L1<B5,B1:L1)),B1:L2,2,FALSE),HLOOKUP(MIN(IF(B1:L1>B5,B1:L1)),B1:L2,2,FALSE)),CHOOSE({1,2},MAX(IF(B1:L1<B5,B1:L1)),MIN(IF(B1:L1>B5,B1:L1))))
Result:

How Can I Round Prices to the nearest 0.95 with an Excel Formula?

I need to round up all my prices to the nearest $0.95. For instance I have 23.106 and 102.58888. They would need to be rounded off to $22.95 and $102.95. How can I do this with an excel formula/function?
Try this:
=IF(OR(A3-FLOOR(A3,1)>0.95,A3=CEILING(A3,1)),CEILING(A3,1)+1,CEILING(A3,1))-0.05
If the decimal is greater than .95 or is a whole number (no decimals), it will add add 1 to the number and then use the CEILING function to round the number up and then subtract .05.
1.52 = 1.95
2.00 = 2.95
66.98 = 67.95

python return wrong result when I multiple float with int

I have a multiple in python 3.7.3
when I run 0.58 * 100 it return 57.99999999999999
Then I found that Java have same result. But C can return right number. I don't know what happen with them. Sorry if it look like basic.
Its actually not the wrong answer, just an unexpected one.
If we think a bit about the problem, There are an infinite amount of numbers between 0 and 1. Then we can see that you cannot represent all numbers between 0 and 1 with a finite amount of bytes, as infinite numbers are more then a finite number of numbers. so some numbers just cant be represented (in fact, most numbers of the infinite series between 0 and 1 cannot be represented)
Following the floating point standard (IEEE-754), 0.58 is really 0.5799999999999999289457264239899814128875732421875 which is the closest number to 0.58 that can be represented with 64bit floating points.
check it with python
>>> Decimal(0.58)
Decimal('0.57999999999999996003197111349436454474925994873046875')
If you want 58.0 you can quantize it to two decimals with the Decimal class.
>>> Decimal(100 * 0.58).quantize(Decimal('.01'))
Decimal('58.00')

Normalized values, when summed are more than 1

I have two files:
File 1:
TOPIC:topic_0 1294
aa 234
bb 123
TOPIC:topic_1 2348
aa 833
cc 239
bb 233
File 2:
0.1 0.2 0.3 0.4
This is just the format of my files. Basically, when the second column (omitting the first "TOPIC" line) is summed for each topic, it constitutes to 1 as they are the normalized values. Similarly, in file 2, the values are normalized and hence they also constitute to 1.
I perform multiplication of the values from file 1 and 2. The resulting output file looks like:
aa 231
bb 379
cc 773
The second column when summed of the output file should give 1. But few files have values little over 1 like 1.1, 1.00038. How can I precisely get 1 for the output file? Is it some rounding off that I should do or something?
PS: The formats are just examples, the values and words are different. This is just for understanding purposes. Please help me sort this.
Python stores floating point decimals in base-2.
https://docs.python.org/2/tutorial/floatingpoint.html
This means that some decimals could be terminating in base-10, but are repeating in base-2, hence the floating-point error when you add them up.
This gets into some math, but imagine in base-10 trying to express the value 2/6. When you eliminate the common factors from the numerator and denominator it's 1/3.
It's 0.333333333..... repeating forever. I'll explain why in a moment, but for now, understand that if only store the first 16 digits in the decimal, for example, when you multiply the number by 3, you won't get 1, you'll get .9999999999999999, which is a little off.
This rounding error occurs whenever there's a repeating decimal.
Here's why your numbers don't repeat in base-10, but they do repeat in base-2.
Decimals are in base-10, which prime factors out to 2^1 * 5^1. Therefore for any ratio to terminate in base-10, its denominator must prime factor to a combination of 2's and 5's, and nothing else.
Now let's get back to Python. Every decimal is stored as binary. This means that in order for a ratio's "decimal" to terminate, the denominator must prime factor to only 2's and nothing else.
Your numbers repeat in base-2.
1/10 has (2*5) in the denominator.
2/10 reduces to 1/5 which still has five in the denominator.
3/10... well you get the idea.

Python 3: How to generate random float (in two decimal places) between 10.00 and 100.00

I need help generating random float (in two decimal places) between 10.00 and 100.00 And I would like to do it without using the print function.
I tried random.choice(range(10.10, 200.00)) but it does not work, because parameters have to be integers.
random.uniform(1, 10) does not seem to have an option for trimming the decimals.

Resources