rounding

How can you divide integers with floor, ceil and outwards rounding modes in C++?

自作多情 提交于 2020-08-19 11:01:12
问题 Recently, I saw this question which asks how you can divide integers with ceil rounding (towards positive infinity). Unfortunately the answers either don't work for signed integers or have problems with underflows and overflows. For instance, the accepted answer has this solution: q = 1 + ((x - 1) / y); When x is zero, there is an underflow to ~0 and the the result is incorrect. How can you implement ceil rounding correctly for signed and unsigned integers and how do you implement other

How can you divide integers with floor, ceil and outwards rounding modes in C++?

|▌冷眼眸甩不掉的悲伤 提交于 2020-08-19 11:00:30
问题 Recently, I saw this question which asks how you can divide integers with ceil rounding (towards positive infinity). Unfortunately the answers either don't work for signed integers or have problems with underflows and overflows. For instance, the accepted answer has this solution: q = 1 + ((x - 1) / y); When x is zero, there is an underflow to ~0 and the the result is incorrect. How can you implement ceil rounding correctly for signed and unsigned integers and how do you implement other

Grouped mean of difftime fails in data.table

爱⌒轻易说出口 提交于 2020-08-02 08:39:15
问题 Preface: I have a column in a data.table of difftime values with units set to days. I am trying to create another data.table summarizing the values with dt2 <- dt[, .(AvgTime = mean(DiffTime)), by = Group] When printing the new data.table, I see values such as 1.925988e+00 days 1.143287e+00 days 1.453975e+01 days I would like to limit the decimal place values for this column only (i.e. not setting options() unless I can do this specifically for difftime values this way). When I try to do this

Grouped mean of difftime fails in data.table

你离开我真会死。 提交于 2020-08-02 08:36:55
问题 Preface: I have a column in a data.table of difftime values with units set to days. I am trying to create another data.table summarizing the values with dt2 <- dt[, .(AvgTime = mean(DiffTime)), by = Group] When printing the new data.table, I see values such as 1.925988e+00 days 1.143287e+00 days 1.453975e+01 days I would like to limit the decimal place values for this column only (i.e. not setting options() unless I can do this specifically for difftime values this way). When I try to do this

Grouped mean of difftime fails in data.table

为君一笑 提交于 2020-08-02 08:36:32
问题 Preface: I have a column in a data.table of difftime values with units set to days. I am trying to create another data.table summarizing the values with dt2 <- dt[, .(AvgTime = mean(DiffTime)), by = Group] When printing the new data.table, I see values such as 1.925988e+00 days 1.143287e+00 days 1.453975e+01 days I would like to limit the decimal place values for this column only (i.e. not setting options() unless I can do this specifically for difftime values this way). When I try to do this

Grouped mean of difftime fails in data.table

余生颓废 提交于 2020-08-02 08:36:29
问题 Preface: I have a column in a data.table of difftime values with units set to days. I am trying to create another data.table summarizing the values with dt2 <- dt[, .(AvgTime = mean(DiffTime)), by = Group] When printing the new data.table, I see values such as 1.925988e+00 days 1.143287e+00 days 1.453975e+01 days I would like to limit the decimal place values for this column only (i.e. not setting options() unless I can do this specifically for difftime values this way). When I try to do this

BigDecimal with smart precision (scale) [duplicate]

假如想象 提交于 2020-07-31 04:52:03
问题 This question already has answers here : Removing trailing zeros from BigDecimal in Java (6 answers) Closed 2 years ago . How to set generic scaling according to decimal roundness as a number BigDecimal num = new BigDecimal(25) BigDecimal result = num.divide(new BigDecimal(13), 7, RoundingMode.HALF_EVEN) // 1.9230769 but BigDecimal result = num.divide(new BigDecimal(10), 7, RoundingMode.HALF_EVEN) // 2.5000000 that should be 2.5 Probably analyzing remainder? Is there something in the API for

BigDecimal with smart precision (scale) [duplicate]

梦想与她 提交于 2020-07-31 04:50:08
问题 This question already has answers here : Removing trailing zeros from BigDecimal in Java (6 answers) Closed 2 years ago . How to set generic scaling according to decimal roundness as a number BigDecimal num = new BigDecimal(25) BigDecimal result = num.divide(new BigDecimal(13), 7, RoundingMode.HALF_EVEN) // 1.9230769 but BigDecimal result = num.divide(new BigDecimal(10), 7, RoundingMode.HALF_EVEN) // 2.5000000 that should be 2.5 Probably analyzing remainder? Is there something in the API for