math

Normalize any value in range (-inf…+inf) to (0…1). Is it possible?

亡梦爱人 提交于 2021-01-20 18:43:23
问题 If we have concrete range of max..min value it is quite easy to normalize it to 0..1 float values, but if we don't have concrete limits? Is it possible to build universal function to have output between 0 and 1? In my mind I think it is impossible but I am not math expert. I'm searching for implementation on JavaScript or PHP, but any code on C/C++/Python/Delphi is OK to provide examples ( if there are some ) 回答1: There are many ways to do this. I'll leave out mapping -inf and +inf , which

Normalize any value in range (-inf…+inf) to (0…1). Is it possible?

丶灬走出姿态 提交于 2021-01-20 18:43:15
问题 If we have concrete range of max..min value it is quite easy to normalize it to 0..1 float values, but if we don't have concrete limits? Is it possible to build universal function to have output between 0 and 1? In my mind I think it is impossible but I am not math expert. I'm searching for implementation on JavaScript or PHP, but any code on C/C++/Python/Delphi is OK to provide examples ( if there are some ) 回答1: There are many ways to do this. I'll leave out mapping -inf and +inf , which

Is floating point math broken?

折月煮酒 提交于 2021-01-20 13:53:52
问题 Consider the following code: 0.1 + 0.2 == 0.3 -> false 0.1 + 0.2 -> 0.30000000000000004 Why do these inaccuracies happen? 回答1: Binary floating point math is like this. In most programming languages, it is based on the IEEE 754 standard. The crux of the problem is that numbers are represented in this format as a whole number times a power of two; rational numbers (such as 0.1 , which is 1/10 ) whose denominator is not a power of two cannot be exactly represented. For 0.1 in the standard

Is floating point math broken?

邮差的信 提交于 2021-01-20 13:53:05
问题 Consider the following code: 0.1 + 0.2 == 0.3 -> false 0.1 + 0.2 -> 0.30000000000000004 Why do these inaccuracies happen? 回答1: Binary floating point math is like this. In most programming languages, it is based on the IEEE 754 standard. The crux of the problem is that numbers are represented in this format as a whole number times a power of two; rational numbers (such as 0.1 , which is 1/10 ) whose denominator is not a power of two cannot be exactly represented. For 0.1 in the standard

Dealing with negative exponents without using POW in C++

房东的猫 提交于 2021-01-20 12:22:28
问题 Hey guys i'm working through a book and i'm little baffled with the sub objective of the following question.. Powers of numbers can be calculated by multiplying the number by itself for as many times as the value of the exponent. For example, 2 raised to the power 4 can be calculated by multiplying 2 by itself 4 times to get 16. Write a program that: 1. inputs a double as the base number and an int as the exponent; double base, ans = 0; int exponent; cout << "This program will calculate the

Displaying total amount including money calculations to user

僤鯓⒐⒋嵵緔 提交于 2021-01-05 12:15:48
问题 Note: I'm already using decimal.Decimal What is the best way to perform and display calculations to the end user when you are required to output in the following rough style: Item - £ 70.10 Item - £ 5.67 Item - £ 10.33 -------------- Total £ 86.10 I was always taught in maths as a kid to not round before your final answer, else you'll lose precision, but in this case if you don't it looks like we have calculation errors. as the calculation behind the display is not the same as the user would

Minimum number of circles to cover n points

一世执手 提交于 2021-01-05 07:29:45
问题 What is the minimum number of circles with radius r needed to cover all n points, while the n points are on a straight line? I know that there is a similar question that was asked before here. Minimum number of circles with radius r to cover n points My attempt : I'm trying to solve it in a linear time , I thought about this algorithm : place the first circle in place that solve for the first point. solve for the second point in the minimum number of circles by checking if the distance

Minimum number of circles to cover n points

ⅰ亾dé卋堺 提交于 2021-01-05 07:29:10
问题 What is the minimum number of circles with radius r needed to cover all n points, while the n points are on a straight line? I know that there is a similar question that was asked before here. Minimum number of circles with radius r to cover n points My attempt : I'm trying to solve it in a linear time , I thought about this algorithm : place the first circle in place that solve for the first point. solve for the second point in the minimum number of circles by checking if the distance

Base conversion for an array of integers

China☆狼群 提交于 2021-01-04 09:15:15
问题 I am trying to make a code that converts integers in array to a given base and padding them to make them from the same size. The following code which I manipulated from a code on stackoverflow by Alex Martelli, doesn't work when I apply numpy.vectorize on it, although it works for single arrays: def int2base(x, base,size): ret=np.zeros(size) if x==0: return ret digits = [] while x: digits.append(x % base) x /= base digits.reverse() ret[size-len(digits):]=digits[:] return ret vec_int2base=np

Base conversion for an array of integers

徘徊边缘 提交于 2021-01-04 09:14:17
问题 I am trying to make a code that converts integers in array to a given base and padding them to make them from the same size. The following code which I manipulated from a code on stackoverflow by Alex Martelli, doesn't work when I apply numpy.vectorize on it, although it works for single arrays: def int2base(x, base,size): ret=np.zeros(size) if x==0: return ret digits = [] while x: digits.append(x % base) x /= base digits.reverse() ret[size-len(digits):]=digits[:] return ret vec_int2base=np