rounding

How do I round this binary number to the nearest even

自作多情 提交于 2019-12-31 07:17:13
问题 I have this binary representation of 0.1 : 0.00011001100110011001100110011001100110011001100110011001100110 I need to round it to the nearest even to be able to store it in the double precision floating point. I can't seem to understand how to do that. Most tutorials talk about guard, round and sticky bits - where are they in this representation? Also I've found the following explanation: Let’s see what 0.1 looks like in double-precision. First, let’s write it in binary, truncated to 57

Batch rounding a number

偶尔善良 提交于 2019-12-31 05:49:12
问题 I have a script that calculates disk space in bytes. I have found a way to convert it to megabytes. I have just run it and i got a this: 1867.603187561035 . Is there a way of rounding it is it would be much clearer if it said 1868 . Thanks for any help. 回答1: @echo off set "number_to_round=1867.603187561035" for /f "tokens=1,2 delims=." %%a in ("%number_to_round%") do ( set first_part=%%a set second_part=%%b ) set second_part=%second_part:~0,1% echo %second_part% if defined second_part if

Understanding round half even?

梦想与她 提交于 2019-12-31 04:58:24
问题 When i print (new BigDecimal(5) * new BigDecimal(0.049))​ It gives 0.24500000000000000943689570931383059360086917877197265625 When i round it using the ROUND_HALF_EVEN (new BigDecimal(5) * new BigDecimal(0.049)).setScale(2, BigDecimal.ROUND_HALF_EVEN)​ It prints 0.25 So my confusion is, shouldn't it round to even number, so instead of 0.25 shouldn't it be 0.24. Please help with this confusion. Thanks! 回答1: The REAL issue here is that you used the wrong constructor for the BigDecimal . (new

Issue with precision of Ruby math operations

◇◆丶佛笑我妖孽 提交于 2019-12-31 04:15:09
问题 Do you know how to fix the following issue with math precision? p RUBY_VERSION # => "1.9.1" p 0.1%1 # => 0.1 p 1.1%1 # => 0.1 p 90.0%1 # => 0.0 p 90.1%1 # => 0.0999999999999943 p 900.1%1 # => 0.100000000000023 p RUBY_VERSION # => "1.9.2" p 0.1%1 # => 0.1 p 1.1%1 # => 0.10000000000000009 p 90.0%1 # => 0.0 p 90.1%1 # => 0.09999999999999432 p 900.1%1 # => 0.10000000000002274 回答1: Big Decimal As the man said; Squeezing infinitely many real numbers into a finite number of bits requires an

Efficiently dividing unsigned value by a power of two, rounding up - in CUDA

情到浓时终转凉″ 提交于 2019-12-31 01:46:11
问题 I was just reading: Efficiently dividing unsigned value by a power of two, rounding up and I was wondering what was the fastest way to do this in CUDA. Of course by "fast" I mean in terms of throughput (that question also addressed the case of subsequent calls depending on each other). For the lg() function mentioned in that question (base-2 logarithm of divisor), suppose we have: template <typename T> __device__ int find_first_set(T x); template <> __device__ int find_first_set<uint32_t>

Round off dict values to 2 decimals

孤街醉人 提交于 2019-12-30 11:48:29
问题 I'm having a hard time rounding off values in dicts. What I have is a list of dicts like this: y = [{'a': 80.0, 'b': 0.0786235, 'c': 10.0, 'd': 10.6742903}, {'a': 80.73246, 'b': 0.0, 'c': 10.780323, 'd': 10.0}, {'a': 80.7239, 'b': 0.7823640, 'c': 10.0, 'd': 10.0}, {'a': 80.7802313217234, 'b': 0.0, 'c': 10.0, 'd': 10.9762304}] I need to round off the values to just 2 decimal places. When I try the following: def roundingVals_toTwoDeci(y): for d in y: for k, v in d.items(): v = ceil(v*100)/100

Rounding of nearest 0.5

眉间皱痕 提交于 2019-12-30 10:52:39
问题 I want to be rounded off this way 13.1, round to 13.5 13.2, round to 13.5 13.3, round to 13.5 13.4, round to 13.5 13.5 = 13.5 13.6, round to 14.0 13.7, round to 14.0 13.8, round to 14.0 13.9, round to 14.0 sorry for modification i need in the above way... did this way but not appropriate doubleValue = Math.Round((doubleValue * 2), MidpointRounding.ToEven) / 2; 回答1: If it is required for 13.1, round to 13.5 and 13.9, round to 14.0 , then: double a = 13.1; double rounded = Math.Ceil(a * 2) / 2;

Rounding of nearest 0.5

会有一股神秘感。 提交于 2019-12-30 10:52:38
问题 I want to be rounded off this way 13.1, round to 13.5 13.2, round to 13.5 13.3, round to 13.5 13.4, round to 13.5 13.5 = 13.5 13.6, round to 14.0 13.7, round to 14.0 13.8, round to 14.0 13.9, round to 14.0 sorry for modification i need in the above way... did this way but not appropriate doubleValue = Math.Round((doubleValue * 2), MidpointRounding.ToEven) / 2; 回答1: If it is required for 13.1, round to 13.5 and 13.9, round to 14.0 , then: double a = 13.1; double rounded = Math.Ceil(a * 2) / 2;

Rounding decimals in nested data structures in Python

允我心安 提交于 2019-12-30 08:25:13
问题 I have a program which deals with nested data structures where the underlying type usually ends up being a decimal. e.g. x={'a':[1.05600000001,2.34581736481,[1.1111111112,9.999990111111]],...} Is there a simple pythonic way to print such a variable but rounding all floats to (say) 3dp and not assuming a particular configuration of lists and dictionaries? e.g. {'a':[1.056,2.346,[1.111,10.000],...} I'm thinking something like pformat(x,round=3) or maybe pformat(x,conversions={'float':lambda x:

How can I round a date to the quarter start/end?

大兔子大兔子 提交于 2019-12-30 08:23:15
问题 I need to take a vector of dates and for each date get the first day of the next quarter. (Or rather, to round to the last day of the current quarter, find the first day of the next quarter and take on off is my plan) lubridate will round/ceiling to months, but not quarters Only solution I've found so far is to make a vector listing all the quarter starts from 1970 to 2099 and then search through that to find the minimum date after my date. Clearly, this vectorises and scales badly. I need to