How to round a float without a built-in function (C)? - rounding

I have a function that calculates the average of all elements in an array. It should output a rounded (to the nearest integer) average. How can I round the float value of the average without using the round() function?
Thanks.
The variable 'avg' is defined as float. I tried avg = int(avg) but this method simply dismisses whatever is after the decimal point.

Related

sin function returning numbers outside of range [-1, 1]

I am working in Haskell and have had an error where, for really large floats z, sin(z) returns some value outside of the range [-1, 1].
I'm learning Haskell for the first time, so I have had very little luck debugging and the program just crashes when sin(z) does return a value outside of the above range as sin(z) is an input into another function that only accepts values inside the range [-1, 1].
Additionally, I don't have access to the other function, I only can send in a value, but it keeps crashing when sin(z) returns a number either greater than 1 or less than -1.
Is there any way to figure out why sin(z) is doing this?
The sin :: Double -> Double function returns a number strictly between -1 and 1 for all finite inputs, no matter how large. In particular, for the largest representable finite positive double, it returns a value that's roughly 0.005:
> sin (1.7976931348623157E+308 :: Double)
4.961954789184062e-3
and for the largest representable finite negative double, it returns a value that's the negative of that:
> sin (-1.7976931348623157E+308 :: Double)
-4.961954789184062e-3
What's undoubtedly happening is that your input to sin has exceeded the finite range of Double. For example, the following double isn't actually representable as a finite double and is "rounded" to infinity:
> 1.7976931348623159E+308 :: Double
Infinity
If you feed such an infinite value to sin, you get NaN:
> sin (1.7976931348623159E+308 :: Double)
NaN
which will undoubtedly cause problems when fed to a function expecting finite numbers between -1 and 1. This can be "fixed" with min:
> min (sin (1.7976931348623159E+308 :: Double)) 1
1.0
but this fix is largely useless because you have a much bigger problem going on.
For numbers this large, the precision of a Double is on the order of plus or minus 1e292. That is, two "adjacent" representable finite doubles of this size are about 1e292 apart and the sin of two such numbers might as well be random numbers between -1 and 1, completely unrelated to any calculation you're trying to complete. Whatever you're trying to do with these numbers can't possibly be working as you intend.
It seems like this is a floating point error; see this similar post. So for very large values, the sin function is returning a value slightly above 1, due to rounding errors.
To solve your problem, I would cap the return value at 1. Specifically, return the min 1 (sin z) instead of just the sin z directly.
Edit: replaced max with min.

What is the function of round() with this strange behavior? [duplicate]

This question already has answers here:
Python 3.x rounding behavior
(13 answers)
Closed 3 years ago.
I want to round some int numbers but I came across with the strange feature of round() for example
round(2.1) = 2
round(2.5) = 2 #it rounds to ceil
round(2.7) = 3
it rounds differently with the odd number as follow
round(5.1) = 5
round(5.5) = 6 #it rounds to floor
round(5.7) = 6
it rounds the X.5 to the floor with the x = even numbers but with the X = odd numbers it rounds to the ceil
I want to ask what is the advantage of this round? and where can I use it in our examples ? or what is its usage?
Looks like if it's close it goes to the even option.
From the documentation https://docs.python.org/3/library/functions.html#round
Return number rounded to ndigits precision after the decimal point. If ndigits is omitted or is None, it returns the nearest integer to its input.
For the built-in types supporting round(), values are rounded to the closest multiple of 10 to the power minus ndigits; if two multiples are equally close, rounding is done toward the even choice (so, for example, both round(0.5) and round(-0.5) are 0, and round(1.5) is 2). Any integer value is valid for ndigits (positive, zero, or negative). The return value is an integer if ndigits is omitted or None. Otherwise the return value has the same type as number.
For a general Python object number, round delegates to number.round.
Note The behavior of round() for floats can be surprising: for example, round(2.675, 2) gives 2.67 instead of the expected 2.68. This is not a bug: it’s a result of the fact that most decimal fractions can’t be represented exactly as a float. See Floating Point Arithmetic: Issues and Limitations for more information.

Round time to nearest 10 milliseconds

I have some time data that, without VBA, I need to round to the nearest 10 milliseconds. For example:
input: 01:02:03.017 output: 01:02:03.020
input: 03:12:44.123 output: 03:12:44.120
Current approach is to convert to an integer number of milliseconds; round that to the nearest 10; finally convert back to time:
=ROUND(A1*86400000,10)/86400000
I must be making a really stupid error, just don't see it.
EDIT:
The formula is returning the same value as the input?!?
Change the 10 to 0
By multiplying the value you want the round to the nearest integer, then divide again. By using 10 you are rounding to the 10th decimal place after creating a integer time.
=ROUND(A1*86400000,0)/86400000
Assuming your time data is in column A:
=TEXT(A1,"hh:mm:ss.00")+0
Change the number format with TEXT, then add the ending 0 with +0

Calculations being being rounded SQL Server 2012 [duplicate]

This question already has answers here:
How to get a float result by dividing two integer values using T-SQL?
(10 answers)
Closed 7 years ago.
I am trying to calculate some performance metrics as [RATE] in SQL but no matter what I do I am only getting integer values in return.
In my dataset I have a Numerator and a Denominator both stored as integers that are broken down into groups and subsets. The rate is calculated slightly differently depending on the group and subset. For group 1 the calculation is simply N/D. For group 2 the calculation is (N*1000)/D with the exception of 1 subset which is calculated (N*10000)/D.
I wrote the query as:
SELECT [Group]
,[SubSet]
,[Numerator] N
,[Denominator] D
,CASE WHEN D=0 Then NULL
WHEN [Group]='G1' THEN [N]/[D]
WHEN [SubSet]='S2' THEN ([N]*10000)/[D]
WHEN [SubSet] NOT LIKE 'S2%' AND [G] NOT LIKE 'G1%' THEN ([N]*1000)/[D] as [RATE]
No matter what I do the outcome variables are integers. I tried formatting RATE as varchar, decimal, and float with no success. I tried changing N and D's format to varchar, decimal, and float as well. I tried changing the equations from (N*1000)/D to (N/D)*1000 but still no effect.
What am I missing/doing wrong?
The problem you are having is because SQL is doing integer division, which will only return whole numbers. To get a decimal return value you must have at least one value as a decimal.
Try this:
(CAST([N] as decimal(12,6))/[D]) * 1000
Adjust decimal(12,6) based on the precision you are expecting. 12,6 will return a decimal with 6 digits after the decimal point. If you wanted only 2 decimal places use 16,2.
If you then want to round the calculated value you will need to make use of the ROUND function in SQL.
Round to the second decimal place:
ROUND((CAST([N] as decimal(12,6))/[D]) * 1000, 2)
You need to use CAST:
CAST ((N*1000) AS FLOAT) / D
Hope this helps.
SELECT (n * 1000.0) will do it.

Truncate to the nearest thousandths and ignore the remainder of the value

Can MS Excel do rounding but only up to the nearest thousandths place and ignore the rest of the decimals in a formula? I've tried value, fixed, round and none of these do what I need.
Let's say I have 100 square feet of space and I pay $1.00566399 per sq foot, I need the formula to round only up to the 5 and ignore the rest of the numbers because when you speak on longer terms it makes a difference in rate.
So I should be multiplying 100sf by $1.01 = $101.00 and not get 100sf * 1.00566399 = $101.57
=trunc(A1,5)
If you want to round up, maybe something like
=trunc((A1*10000+1)/10000,5)
Use the TRUNC($cellRef or number, Decimal places) function. It reduces the number of decimal places WITHOUT rounding, then do your math.
So for example:
=TRUNC(1.00566399,3)
=A1*ROUNDUP(B1,2)
Where A1 contains the number of square feet and B1 contains the price per square foot in it's original long decimal form.

Resources