What is the difference in floor function and truncate function? [duplicate] - math

This question already has answers here:
Difference between Math.Floor() and Math.Truncate()
(13 answers)
Closed 5 years ago.
What is the difference between Math.Floor() and Math.Truncate() in .NET?
For example, Math.Floor(4.4) = 4 Math.Truncate(4.4) = 4.

These function behave differently for negative numbers.
Math.Truncate(-4.5) = -4
Math.Floor(-4.5) = -5

Math.Floor rounds down Math.Ceiling rounds up and Math.Truncate rounds towards zero. Thus, Math.Truncate is like Math.Floor for positive numbers, and like Math.Ceiling for negative numbers.

Related

round numbers to non-decimal numbers [duplicate]

This question already has answers here:
Rounding numbers to nearest 10 in R
(2 answers)
Closed 1 year ago.
How can I round the following numbers:
16554.2644 and 2238.987
to:
16550 and 2240
and
16560 and 2230
Thanks
Divide the number by 10, round using either floor or ceil and then multiply by 10.

In 64 bit floating point why does sqrt(2^52+1)-sqrt(2^52) equal 0 [duplicate]

This question already has answers here:
Why (1UL <<53) plus 1.0 does not equal to itself?
(2 answers)
Is floating point math broken?
(31 answers)
Closed 1 year ago.
Why is it that when I put this equation in matlab it equals 0 but when I do (2^52+1) -2^52 it gives me 1, aren't they the same equation. I think I kind of understand how 64bit float can't represent odd numbers past 2^53-1 but these numbers are not even close to that
There are 2^52 double precision numbers between 2^52 and 2^53-1 which the sqrt function maps to (approximately) the interval [2^26, sqrt(2)*2^26]. The latter contains just 2^52/sqrt(2) numbers, which means a lot of numbers x must map to the same sqrt(x).

How can I consistently round up .5 decimal values ​in R? [duplicate]

This question already has answers here:
Round up from .5
(7 answers)
Closed 3 years ago.
The help of round states that ‘go to the even digit’ is used for rounding.
How can I have a consistent rounding mechanism?
For example:
This rounds up.
round(1.5, 0)
2
This rounds down.
round(4.5, 0)
4
In both cases I want it to round up.
But I also want 1,4 to round down and 1,7 to round up.
There is the functions floor() and ceiling()
floor(1.5)
1
ceiling(1.5)
2

Transform a vector to run to its max value without decreasing [duplicate]

This question already has answers here:
Calculating the maximum of sub-vectors of a larger vector
(2 answers)
Closed 5 years ago.
I am looking for a clever way to transform a vector into a version of itself which runs to its maximum value without ever decreasing, and which plateaus once it reaches its maximum.
So, for input like:
x <- c(1,2,6,3,2,9,8,4)
I'd like output like:
y <- c(1,2,6,6,6,9,9,9)
I find this easy to do with a loop, but I am trying to do this to 1000's of reasonably large vectors, so I'd really prefer a more efficient solution. Thank you in advance!
We can use cummax to do this
cummax(x)
#[1] 1 2 6 6 6 9 9 9

sum with n decimal places R [duplicate]

This question already has answers here:
Controlling number of decimal digits in print output in R
(4 answers)
Closed 9 years ago.
I am trying to have sum of two values in R with 6 decimal places but it only returns with 5.
85.85+0.01302778
# [1] 85.86303
I tried
round(85.85+0.01302778,6)
but it does not work.
Actually sum(85.85,0.01302778) gives only 5 decimals and I did not find any scope of decimal places in ?sum.
Any suggestions
Try this to get 6 digits:
> options(digits=8)
> 85.85+0.01302778
[1] 85.863028

Resources