rounding

Rounding oddity - what is special about “100”?

假如想象 提交于 2020-01-26 10:53:52
问题 Does anyone have an explanation for this strange rounding in haskell (GHCi, version 7.2.1). Everything seems fine unless I multiply with 100. *Main> 1.1 1.1 *Main> 1.1 *10 11.0 *Main> 1.1 *100 110.00000000000001 *Main> 1.1 *1000 1100.0 *Main> 1.1 *10000 11000.0 Edit: what is puzzeling me is that the rounding error only shows when multiplying with 100. Edit(2): The comments I received made me realize, that this it totally unrelated to haskell, but a general issue with floating point numbers.

Is there an “inverted” trunc function in C or C++? [duplicate]

亡梦爱人 提交于 2020-01-25 07:57:11
问题 This question already has answers here : c++ rounding of numbers away from zero (5 answers) Closed 8 hours ago . Is there a function in C or C ++ - similar to trunc - that rounds off negative numbers and rounds up positive numbers? Like in this example: -3.3 to -4 or 2.1 to 3 I could only find the "inverse" function trunc. But can hardly believe that this does not exist. Do I really have to first query the positivity via if and then round it up accordingly? I need this because I have the sign

python 2.7: round a float up to next even number

╄→гoц情女王★ 提交于 2020-01-24 02:25:07
问题 I would like to round up a float to the next even number. Steps: 1) check if a number is odd or even 2) if odd, round up to next even number I have step 1 ready, a function which checks if a give number is even or not: def is_even(num): if int(float(num) * 10) % 2 == 0: return "True" else: return "False" but I'm struggling with step 2.... Any advice? Note: all floats will be positive. 回答1: There is no need for step 1. Just divide the value by 2, round up to the nearest integer, then multiply

Why is C# Rounding This Way?

偶尔善良 提交于 2020-01-23 13:09:28
问题 So, I've got this floating point number: (float)-123456.668915 It's a number chosen at random because I'm doing some unit testing for a chunk of BCD code I'm writing. Whenever I go to compare the number above with a a string ( "-123456.668915" to be clear), I'm getting an issue with how C# rounded the number. It rounds it to -123456.7. This has been checked in NUnit and with straight console output. Why is it rounding like this? According to MSDN, the range of float is approximately -3.4 * 10

Why is C# Rounding This Way?

若如初见. 提交于 2020-01-23 13:09:08
问题 So, I've got this floating point number: (float)-123456.668915 It's a number chosen at random because I'm doing some unit testing for a chunk of BCD code I'm writing. Whenever I go to compare the number above with a a string ( "-123456.668915" to be clear), I'm getting an issue with how C# rounded the number. It rounds it to -123456.7. This has been checked in NUnit and with straight console output. Why is it rounding like this? According to MSDN, the range of float is approximately -3.4 * 10

How can I round to a certain floating-point precision?

▼魔方 西西 提交于 2020-01-23 11:23:44
问题 I think it's a simple question. I want: a = 1.154648126486416; to become: a = 1.154; and not: a = 1.15000000000; How do I do that without using format('bank') . 回答1: You could do this: a = floor(a*1000)/1000; 回答2: Building on @gnovice's answer, you can format the output as a string to get rid of the extra zeros. See the sprintf documentation for all the formatting options. str=sprintf('The result is %1.3f.',a); disp(str) will show "The result is 1.154." in the command prompt. Or write the

Rounding Half Up with Decimal Format in Android

故事扮演 提交于 2020-01-23 01:26:08
问题 I want to set the Rounding Mode to HALF_UP on my DecimalFormat, but eclipse is telling me that setRoundingMode() is not available on the DecimalFormat class. My project properties (and the overall Eclipse properties) are using the 1.6 compiler. The developer.android.com site says that I can use either Java 5 or 6 so I'm not sure what the problem is. import java.math.RoundingMode; import java.text.DecimalFormat; completedValueFormatter = NumberFormat.getNumberInstance(); DecimalFormat

round double to 0.5

你说的曾经没有我的故事 提交于 2020-01-22 19:00:38
问题 i have a result "1.444444" and i want to round this result to "1.5" this is the code i use : a.text = String(round(13000 / 9000.0)) but this is round to "1.0" and i need round to "1.5" and this code : a.text = String(ceil(13000 / 9000.0)) is round to "2.0" 回答1: x = 13000 / 9000.0; denominator = 2; a.text = String(round(x*denominator )/denominator ); First convert 1.444 to 2.888, then round to 3.0 and then divide by 2 to get 1.5. In this case, the denominator of 0.5 is 2 (i.e. 1/2). If you

Android number format is wrong somehow, instead of 3.5 I get 3.499999999, why?

江枫思渺然 提交于 2020-01-21 14:39:12
问题 I store some data in a database and I read those data with a cursor. All the data are 56.45 , 3.04, 0.03 type, so two numbers after the decimal point. Now I would like to sum them but it doesnt seem to be easy... I get those number with c.getDouble(3) then I add it to the sum variable like: sum+= c.getDouble(4) * multi multi is an integer between 1 and 100. Now my problem is that, after getting the sum variable I get a number like: 59.51999999999999999. I do not post a code, but there is no

Round currency closest to five

只谈情不闲聊 提交于 2020-01-21 11:55:25
问题 I'd like to round my values to the closest of 5 cent for example: 5.31 -> 5.30 5.35 -> 5.35 5.33 -> 5.35 5.38 -> 5.40 Currently I'm doing it by getting the decimal values using: let numbers = 5.33 let decimal = (numbers - rint(numbers)) * 100 let rounded = rint(numbers) + (5 * round(decimal / 5)) / 100 // This results in 5.35 I was wondering if there's a better method with fewer steps because sometimes numbers - rint(numbers) is giving me a weird result like: let numbers = 12.12 let decimal =