decimal

How to judge a int number odd or even? (the binary way)

心不动则不痛 提交于 2019-12-08 17:23:01
问题 I wanna use basic knowledge to improve the efficiency of code. I know that in binary system. when the last digit of number is 1,this is a odd number.and 0 is even number. How to use this way to judge a int number in python? Is that python give any build-in method to do it? 回答1: AND it with 1: 0000101001000101 0000000000000001 & __________________ 0000000000000001 If you get 1 , the number is odd. If you get 0 , the number is even. While this works, I would use the modulo operator instead: >>>

C# Decimal.GetHashCode() and Double.GetHashCode() equal

旧城冷巷雨未停 提交于 2019-12-08 16:23:38
问题 Why is it that 17m.GetHashCode() == 17d.GetHashCode() (m=decimal, d=double) Additionally, as expected 17f.GetHashCode() != 17d.GetHashCode() (f=float) This appears to be true for both net3.5 and net4.0. As I understand, the internal bit representations of these types are quite different. So how come that the hash codes of decimal and double types equal for equal initialization values? Is there some conversion taking place before calculation of the hash? I found that the source code for Double

Format decimal as a percent with specific decimal places

那年仲夏 提交于 2019-12-08 16:23:08
问题 <%= Model.STPData.InitialRateSetting.HasValue ? Model.STPData.InitialRateSetting.Value.ToString() : "" %> The Model.STPData.InitialRateSetting is a decimal. I want to format that as a percentage, and then round it to 5 decimal places. How would I do that? 回答1: you can use Model.STPData.InitialRateSetting.Value.ToString("P5"); assuming InitialRateSetting is a decimal 回答2: You could use ToString("p5") . This will take the number 0.051234567 and display it as "5.12346%". I'm not sure if it'll

getting Ceil() of Decimal in python?

你离开我真会死。 提交于 2019-12-08 16:21:17
问题 Is there a way to get the ceil of a high precision Decimal in python? >>> import decimal; >>> decimal.Decimal(800000000000000000001)/100000000000000000000 Decimal('8.00000000000000000001') >>> math.ceil(decimal.Decimal(800000000000000000001)/100000000000000000000) 8.0 math rounds the value and returns non precise value 回答1: x = decimal.Decimal('8.00000000000000000000001') with decimal.localcontext() as ctx: ctx.prec=100000000000000000 ctx.rounding=decimal.ROUND_CEILING y = x.to_integral_exact

how to keep decimals in c

半城伤御伤魂 提交于 2019-12-08 13:36:49
问题 I am writing simple code for homework. I get one number from user, which is 3.4, when I define it with scanf("%d",&a) it takes only 3 and do it that way. I defined a as int a; What should I do? 回答1: Use float float a; scanf("%f", &a); 回答2: I think you are very new to c programming. This is a very simple job. This can be done as:- float a; // to input a data in the variable a scanf("%f",&a); //to display the stored data printf("%f\n",a); //or printf("%.nf\n",a);//specify value of n //maximum

Calculate which numbers cause problems when converting decimal to floating point?

戏子无情 提交于 2019-12-08 12:32:41
问题 I know when converting some numbers from binary to floating point there can be problems. For example: 1.4 results in 1.39999. 6.2 is 6.1999999, 6.6 is 6.599999, etc. Is there a way to calculate which numbers will cause these problems? Like create some sort of spreadsheet or database with numbers from 1-50,000 which don't convert exactly? 回答1: The errors in floating point calculations are rooted in the way that floating point numbers are stored. Depending on which precision you are using

Python decimal.Decimal id not the same

送分小仙女□ 提交于 2019-12-08 06:48:08
问题 I am having a problem with encoding to JSON in Python, specifically with decimal.Decimal values. I am using this to output JSON for a Google App Engine application. To circumvent the exception from the default json module in Python telling me that it can't handle decimal.Decimal objects, I am using this encoder subclass: class DecimalEncoder(json.JSONEncoder): def default(self, o): if isinstance(o, decimal.Decimal): return float(o) return super(DecimalEncoder, self).default(o) On other

How to represent floating point in binary. IEEE

旧城冷巷雨未停 提交于 2019-12-08 06:01:35
问题 Similar to decimal binary numbers can have represent floats too. Now I read that it can have floats of the sort 0.5 : 0.1 , 0.25 : 0.01 , 0.125 : 0.001 ... and so on. But then, for example, how is the 0.1(in decimal) represented in binary? Also, given a decimal float, how to convert it to the decimal equivalent, (given it is not so straightforward). Edit: So I understand that the better question would have been ; how to convert a decimal float to binary? Now i get it that we multiply the

Decimal to Binary conversion method Objective-C

时光毁灭记忆、已成空白 提交于 2019-12-08 02:43:27
问题 Hi I am trying to make a Decimal to binary number converter in Objective-C but have been unsucessful... I have the following method so far which is an attempted translation from Java for a similar method. Any help to make this method work is much appreciated. +(NSString *) DecToBinary: (int) decInt { int result = 0; int multiplier; int base = 2; while(decInt > 0) { int r = decInt % 2; decInt = decInt / base; result = result + r * multiplier; multiplier = multiplier * 10; } return [NSString

C Fast base convert from decimal to ternary

三世轮回 提交于 2019-12-08 02:03:35
问题 is there any method of changing decimal number to ternary ? I mean i don't want to use modulo and divide method, i have very big decimal number, something like 128123832812381835828638486384863486.............1237127317237 and so on. Also don't want to use bigints. Is there any method ? 回答1: You don't have to use divide/modulo. Instead, iterate over the input digits, from low to high. For each digit position, first calculate what 1000....000 would be in the output representation (it's 10x the