rounding of digits

altimit

I'm having troubles with

set.seed(1)
sum(abs(rnorm(100)))
set.seed(1)
cumsum(abs(rnorm(100)))

Why does the value of the sum differ from the last value of the cumulative sum with the cumulative sum preserving the all decimal digits and sum rounding 1 digit off.

Rich Scriven

This is a consequence of the way R prints atomic vectors.

With the default digits option set to 7 as you likely have, any value between -1 and 1 will print with seven decimal places. Because of the way R prints atomic vectors, all other values in the vector will also have seven decimal places. Furthermore, a value of .6264538 with digits option set to 7 must print with eight digits (0.6264538) because it must have a leading zero. There are two of these values in your rnorm() vector.

If you look at cumsum(abs(rnorm(100)))[100] alone and you can see the difference (actually it becomes the same as printed value as sum(abs(rnorm(100))), although not exactly the same value).

sum(abs(rnorm(100)))
# [1] 71.67207
cumsum(abs(rnorm(100)))[100]
# [1] 71.67207

Notice that both of these values have seven digits. Probably the most basic example of this I can think of is as follows

0.123456789
#[1] 0.1234568
1.123456789
#[1] 1.123457
11.123456789
# [1] 11.12346
## and so on ...

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

rounding of digits

From Dev

Rounding to n significant digits

From Dev

Rounding digits in ggpairs

From Dev

Rounding and trimming digits with specified precision

From Dev

Rounding to specific number of digits in Haskell

From Dev

Rounding to specific number of digits in Haskell

From Dev

Rounding Logic for decimal digits in java

From Dev

rounding to desired digits in MATLAB and C

From Dev

Rounding numbers to 2 digits after comma in oracle

From Dev

Double to Decimal without rounding after 15 digits

From Dev

PHP: rounding a number into 16 decimal digits

From Dev

Controlling digits / rounding as argument in base functions

From Dev

PHP: rounding a number into 16 decimal digits

From Dev

Rounding numbers to 2 digits after comma in oracle

From Dev

rounding 1 to have two floating digits

From Java

String.format() rounding a double with leading zeros in digits - Java

From Dev

Rounding to specfic digits fails with this double-precision value

From Dev

Java, how to truncate digits in a long such in a way that mimics rounding?

From Dev

How to display a fixed number of digits in C++ without rounding

From Dev

MySQL, change default rounding to more than four digits

From Dev

Java BigDecimal: Rounding off to client preferred digits and increment

From Dev

Rounding floating variables in C to display upto only 2 digits of precision

From Dev

show 4 digits after a decimal without rounding the values

From Dev

How to truncate a double to four digits in hive without rounding?

From Dev

R - Vary by Row the Rounding of Digits Produced by knitr::kable

From Dev

How to truncate a double to four digits in hive without rounding?

From Dev

Rounding to specfic digits fails with this double-precision value

From Dev

How to write string with 3 significant digits without rounding

From Dev

Rounding errors to 1 significant figure and round values according to number of digits in errors

Related Related

  1. 1

    rounding of digits

  2. 2

    Rounding to n significant digits

  3. 3

    Rounding digits in ggpairs

  4. 4

    Rounding and trimming digits with specified precision

  5. 5

    Rounding to specific number of digits in Haskell

  6. 6

    Rounding to specific number of digits in Haskell

  7. 7

    Rounding Logic for decimal digits in java

  8. 8

    rounding to desired digits in MATLAB and C

  9. 9

    Rounding numbers to 2 digits after comma in oracle

  10. 10

    Double to Decimal without rounding after 15 digits

  11. 11

    PHP: rounding a number into 16 decimal digits

  12. 12

    Controlling digits / rounding as argument in base functions

  13. 13

    PHP: rounding a number into 16 decimal digits

  14. 14

    Rounding numbers to 2 digits after comma in oracle

  15. 15

    rounding 1 to have two floating digits

  16. 16

    String.format() rounding a double with leading zeros in digits - Java

  17. 17

    Rounding to specfic digits fails with this double-precision value

  18. 18

    Java, how to truncate digits in a long such in a way that mimics rounding?

  19. 19

    How to display a fixed number of digits in C++ without rounding

  20. 20

    MySQL, change default rounding to more than four digits

  21. 21

    Java BigDecimal: Rounding off to client preferred digits and increment

  22. 22

    Rounding floating variables in C to display upto only 2 digits of precision

  23. 23

    show 4 digits after a decimal without rounding the values

  24. 24

    How to truncate a double to four digits in hive without rounding?

  25. 25

    R - Vary by Row the Rounding of Digits Produced by knitr::kable

  26. 26

    How to truncate a double to four digits in hive without rounding?

  27. 27

    Rounding to specfic digits fails with this double-precision value

  28. 28

    How to write string with 3 significant digits without rounding

  29. 29

    Rounding errors to 1 significant figure and round values according to number of digits in errors

HotTag

Archive