decimal

Why doesn't python decimal library return the specified number of signficant figures for some inputs

人走茶凉 提交于 2019-12-07 12:01:16
问题 NB : this question is about significant figures. It is not a question about "digits after the decimal point" or anything like that. EDIT : This question is not a duplicate of Significant figures in the decimal module. The two questions are asking about entirely different problems. I want to know why the function about does not return the desired value for a specific input. None of the answers to Significant figures in the decimal module address this question. The following function is

NumberFormatException on European Versions of Android?

删除回忆录丶 提交于 2019-12-07 11:58:44
问题 I have an app which runs the following two lines of code upon starting: DecimalFormat decim = new DecimalFormat("#.00"); return Double.parseDouble(decim.format(totalNumberOfCredits)); When I start the app on my American phone, the value of decim.format(totalNumberOfCredits) is .00 . However, in my Google Play Developer Console, I have a dozen crashes, all of which look like this: Caused by: java.lang.NumberFormatException: Invalid double: ",00" at java.lang.StringToReal.invalidReal

SQL Numeric data type truncating value?

☆樱花仙子☆ 提交于 2019-12-07 11:55:16
问题 I really hope some SQL guru out there can assist with this one (and my apologies if this has been answered before. I did try and find a similar post but to no avail): declare @theanswer numeric(38,16) select @theanswer = 0.01 / 0.0074464347 select @theanswer The above results in 1.3429245542165000 but the following (which looks the same to me) declare @val1 numeric(38,16); declare @val2 numeric(38,16); set @val1 = 0.01; set @val2 = 0.0074464347; select @val1/@val2 results with 1.342924 and

Overflow Exception when dividing two decimals in .NET

自闭症网瘾萝莉.ら 提交于 2019-12-07 07:28:38
问题 I'm having an issue trying to divide two decimals and then display the result. Annoyingly this is only happening on our server, and it appears to work perfectly fine if I run the code locally. This is the code that I am trying to run decimal dOne = -966.96M; decimal dTwo = 2300M; decimal dResult = Decimal.Round((dOne / dTwo), 28, MidpointRounding.AwayFromZero); The resulting number (as generated from windows calculator) is -0.43346086956521739130434782608696 This always results in an overflow

Java DecimalFormat Scientific Notation Question

狂风中的少年 提交于 2019-12-07 06:33:19
问题 I'm using Java's DecimalFormat class to print out numbers in Scientific Notation. However, there is one problem that I have. I need the strings to be of fixed length regardless of the value, and the sign on the power of ten is throwing it off. Currently, this is what my format looks like: DecimalFormat format = new DecimalFormat("0.0E0"); This gives me the following combinations: 1.0E1, 1.0E-1, -1.0E1, and -1.0E-1. I can use setPositivePrefix to get: +1.0E1, +1.0E-1, -1.0E1, and -1.0E-1, or

How use the mean method on a pandas TimeSeries with Decimal type values?

吃可爱长大的小学妹 提交于 2019-12-07 05:30:07
问题 I need to store Python decimal type values in a pandas TimeSeries / DataFrame object. Pandas gives me an error when using the "groupby" and "mean" on the TimeSeries/DataFrame. The following code based on floats works well: [0]: by = lambda x: lambda y: getattr(y, x) [1]: rng = date_range('1/1/2000', periods=40, freq='4h') [2]: rnd = np.random.randn(len(rng)) [3]: ts = TimeSeries(rnd, index=rng) [4]: ts.groupby([by('year'), by('month'), by('day')]).mean() 2000 1 1 0.512422 2 0.447235 3 0

Decimal order of addition affects results

那年仲夏 提交于 2019-12-07 04:54:57
问题 I have a system that is performing lots of calculations using decimals, occasionally it will add up the same numbers, but return different results, +/- 0.000000000000000000000000001 Here is a short example: decimal a = 2.016879990455473621256359079m; decimal b = 0.8401819425625631128956517177m; decimal c = 0.4507062854741283043456903406m; decimal d = 6.7922317815078349615022988627m; decimal result1 = a + b + c + d; decimal result2 = a + d + c + b; Console.WriteLine((result1 == result2) ?

How deal with the fact that most decimal fractions cannot be accurately represented in binary?

泄露秘密 提交于 2019-12-07 04:48:39
问题 So, we know that fractions such as 0.1, cannot be accurately represented in binary base, which cause precise problems (such as mentioned here: Formatting doubles for output in C#). And we know we have the decimal type for a decimal representation of numbers... but the problem is, a lot of Math methods, do not supporting decimal type, so we have convert them to double, which ruins the number again. so what should we do? 回答1: For a comprehensive examination of the challenges involved in

C++ How to output number with at least one number behind the decimal mark

蓝咒 提交于 2019-12-07 03:08:09
问题 How can I make my program output a number with at least one number behind the decimal mark C++? Output: 1 = 1.0 or 1.25 = 1.25 or 2.2 = 2.2 or 3.456789 = 3.456789 Thanks in advance 回答1: Use showpoint to force the decimal point to be printed double x = 1.0; std::cout << std::showpoint << x << "\n"; It will be followed by the number of 0 required to satisfy the precision of the stream. 回答2: #include <cmath> #include <iostream> #include <limits> struct FormatFloat { static constexpr const double

Modulo operation on a python negative decimal.Decimal and a positive int

≯℡__Kan透↙ 提交于 2019-12-07 02:48:28
问题 With simple int s: >>> -45 % 360 315 Whereas, using a decimal.Decimal : >>> from decimal import Decimal >>> Decimal('-45') % 360 Decimal('-45') I would expect to get Decimal('315') . Is there any reason for this? Is there a way to get a consistent behaviour (without patching decimal.Decimal )? (I did not change the context, and cannot find how it could be changed to solve this situation). 回答1: Python behaves according to IBM's General Decimal Arithmetic Specification. The remainder is defined