decimal

Optimized algorithm for converting a decimal to a “pretty” fraction

巧了我就是萌 提交于 2019-12-05 11:06:22
Rather than converting an arbitrary decimal to an exact fraction (something like 323527/4362363), I am trying to convert to just common easily-discernible (in terms of human-readability) quantities like 1/2, 1/4, 1/8 etc. Other than using a series of if-then, less than/equal to etc comparisons, are there more optimized techniques to do this? Edit: In my particular case, approximations are acceptable. The idea is that 0.251243 ~ 0.25 = 1/4 - in my usage case, that's "good enough", with the latter more preferable for human readability in terms of a quick indicator (not used for calculation, just

Java DecimalFormat Scientific Notation Question

穿精又带淫゛_ 提交于 2019-12-05 10:49:43
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 whatever I like, but it doesn't affect the sign of the power! Is there any way to do this so that I can

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

╄→гoц情女王★ 提交于 2019-12-05 10:41:08
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.290151 4 -0.227240 5 0.078815 6 0.396150 7 -0.507316 But i get an error if do the same using decimal values

Calculating nth root in Java using power method

此生再无相见时 提交于 2019-12-05 10:40:16
问题 I was trying to get a cubic root in java using Math.pow(n, 1.0/3) but because it divides doubles, it doesn't return the exact answer. For example, with 125, this gives 4.9999999999. Is there a work-around for this? I know there is a cubic root function but I'd like to fix this so I can calculate higher roots. I would not like to round because I want to know whether a number has an integer root by doing something like this: Math.pow(n, 1.0 / 3) % ((int) Math.pow(n, 1.0 / 3)) . 回答1: Since it is

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

隐身守侯 提交于 2019-12-05 09:40:18
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? For a comprehensive examination of the challenges involved in performing floating-point calculations, see this article: What Every Computer Scientist Should Know About Floating

Convert hex to decimal in R

孤人 提交于 2019-12-05 09:13:05
问题 I found out that there is function called .hex.to.dec in the fBasics package. When I do .hex.to.dec(a) , it works. I have a data frame with a column samp_column consisting of such values: a373, 115c6, a373, 115c6, 176b3 When I do .hex.to.dec(samp_column) , I get this error: "Error in nchar(b) : 'nchar()' requires a character vector" When I do .hex.to.dec(as.character(samp_column)) , I get this error: "Error in rep(base.out, 1 + ceiling(log(max(number), base = base.out))) : invalid 'times'

Parse string to decimal, commas and periods

左心房为你撑大大i 提交于 2019-12-05 08:17:14
How to parse string to decimal so it would work for both formats - w/ commas and periods? [Fact] public void foo(){ var a="1,1"; var b="1.1"; Assert.Equal(Parse(a),Parse(b)); } private decimal Parse(string s){ return decimal.Parse(s,NumberStyles.Any, CultureInfo.InvariantCulture); } output: Test 'Unit.Sandbox.foo' failed: Assert.Equal() Failure Expected: 11 Actual: 1,1 You could try that: private decimal Parse(string s){ s = s.Replace(",", CultureInfo.InvariantCulture.NumberFormat.NumberDecimalSeparator); return decimal.Parse(s,NumberStyles.Any, CultureInfo.InvariantCulture); } Bat_Programmer

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

a 夏天 提交于 2019-12-05 07:18:44
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 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. #include <cmath> #include <iostream> #include <limits> struct FormatFloat { static constexpr const double precision = std::sqrt(std::numeric_limits<double>::epsilon()); const double value; FormatFloat(double value) :

Convert binary vector to decimal

五迷三道 提交于 2019-12-05 07:03:00
I have a vector of a binary string: a<-c(0,0,0,1,0,1) I would like to convert this vector into decimal. I tried using the compositions package and the unbinary() function, however, this solution and also most others that I have found on this site require g-adic string as input argument. My question is how can I convert a vector rather than a string to decimal? to illustrate the problem: library(compositions) unbinary("000101") [1] 5 This gives the correct solution, but: unbinary(a) unbinary("a") unbinary(toString(a)) produces NA. You could try this function bitsToInt<-function(x) { packBits

validate decimal numbers

为君一笑 提交于 2019-12-05 06:40:46
问题 I want to validate that a number has certain parameters, for example I want to ensure that a number has 3 decimals is positive. I have searched in different places over the internet, although I could not find how to do it. I have made that text box to accept numbers only. I just need the rest of the features. Thanks, $("#formEntDetalle").validate({ rules: { tbCantidad: { required: true, number: true }, tbPrecioUnidad: { required: true, number: true }, } messages: { tbCantidad: { required: "Es