Questions tagged [math]

Programming questions that are significantly affected or best defined by the underlying mathematics of the problem.

Filter by
Sorted by
Tagged with
16 votes
11 answers
16k views

Is it possible to build a system to generate UUIDs where every UUID is guaranteed unique?

Is it possible to design a distributed system to generate unique ids where there is a requirement that every generated id is guaranteed to be unique with no possibility of collision? I didn't think ...
0 votes
0 answers
17 views

Feedback on API design of a HalfEdge Data structure

Background Sometimes also referred to as doubly connected edge list, the HalfEdge data structure allows programmers to efficiently and easily modify topological and geometric properties of a mesh. ...
1 vote
2 answers
1k views

Loan Amortization Schedule - Precision and rounding

I have been playing around making amortization schedules in PHP. My php.ini currently has the precision set to 14. I understand going into this that there will be rounding errors however I am hoping ...
22 votes
6 answers
5k views

Why is mod (%) a fundamental mathematical operator in many programming languages?

Is there a reason, historical or otherwise, why the modulus operator is part of a small set of standard operators in what seems like many languages? (+, -, *, / and %, for Java and C, with ** in Ruby ...
11 votes
1 answer
13k views

Can one edit a built-in Python module?

I'm currently learning Python and I'm at the point in the book about using the Math library. I looked on the Python website and noticed the library was a bit scarce and am writing some more useful ...
31 votes
17 answers
5k views

Introducing Programming To a Mathematician [closed]

I currently am a programmer, I'm almost 16 years of age and have pretty much narrowed my careers down to something involving a Computer Science degree or Electrical Engineering degree (I know they are ...
2 votes
2 answers
1k views

Why are floats called "real numbers" in some languages?

Some programing languages, notably Pascal, have a type of numbers called "real". However, mathematically speaking, these types aren't real. For them to be "real", these types have to be able to ...
6 votes
1 answer
2k views

Fast fixed-size (256 bit) integer modulo/division

What is the fastest way to divide two 256-bit integers? They are represented as an array of 26-bit words, each stored in 32-bit integers. I have access to a 64-bit type if that's useful. It seems ...
12 votes
5 answers
11k views

Convert version string to integer

I need to be able to convert a version string to an integer. I've decided I will follow semantic versioning so version strings will be of the type x.y.z. Initially I thought a simple algorithm like ...
3 votes
4 answers
3k views

What are the advantages of matrix multiplication over individual variables (graphics)?

I am learning OpenGL and the tutorials (1, 2) I'm reading teach me that to scale/rotate/translate an object you have to know matrix multiplication. Why? Instead of 3x3 matrix you can use 6 floats: ...
2 votes
3 answers
6k views

Algorithm to calculate trajectories from vector field

I have a two-dimensional vector field, i.e., for each point (x, y) I have a vector (u, v), whereas u and v are functions of x and y. This vector field canonically defines a set of trajectories, i.e. ...
1 vote
1 answer
89 views

Ranking results from a Question and Answer game

I have a question and answer trivia game app which randomly picks questions from a database and prompts the user to answer the question correctly. The total number of correct answers, the total number ...
-1 votes
1 answer
501 views

Is there a term for "shift towards zero"?

In pseudo code: given numbers x and y, if x is greater than zero, subtract the magnitude of y from x, otherwise add the magnitude of y to x. It's ok if x goes from being positive to negative or vice ...
59 votes
5 answers
55k views

get weighted random item

I have, for example, this table +-----------------+ | fruit | weight | +-----------------+ | apple | 4 | | orange | 2 | | lemon | 1 | +-----------------+ I need to return a random ...
7 votes
7 answers
8k views

What is the size of the number 65535 in bytes? [closed]

As I got to know there are 256 possible combinations to get for 1 byte. If I understand it correctly, it should mean that you can display any number out of numbers 0-255 and this very number would use ...
2 votes
2 answers
129 views

Design a function that indicates significant deviations in response times

I'd need some feedback on how to approach the design of a function that highlights parts of a time series chart. The chart shows the response time of an application, in particular the 90th percentile, ...
14 votes
3 answers
14k views

Find the new coordinates using a starting point, a distance, and an angle

Okay, say I have a point coordinate. var coordinate = { x: 10, y: 20 }; Now I also have a distance and an angle. var distance = 20; var angle = 72; The problem I am trying to solve is, if I want ...
86 votes
6 answers
33k views

Why is negative zero important?

I'm confused about why we care about different representations for positive and negative zero. I vaguely recall reading claims that having a negative zero representation is extremely important in ...
0 votes
1 answer
126 views

Understand implementation of exponential moving average (in case of unix load average)

The UNIX load average gives 3 numbers over 1/5/15 minute time intervals. It's supposed to be an indicator of how busy a UNIX machine is. The global load average is an exponentially decaying average of ...
-1 votes
1 answer
2k views

How can I create a set of pixel coordinates for a line in Python?

I've got some code that's searching pixel by pixel from a starting point, at a certain angle, until it finds a pixel (a non-white pixel) It only has to search around 300 pixels: I'm currently using ...
2 votes
6 answers
607 views

What is the viability of engineering an integral type with values ranging from -1 to 254? Do types like this even exist?

In software engineering, often developers will utilize three different states of a signed integer, as a trilean: This tends to be quite typical: -1 - Represents something akin to a null pointer, as in ...
4 votes
3 answers
253 views

Is it possible to store N bits of unique combinations, in N-1 bits? If not; why does MD5 get reprimanded for collissions?

Regarding cryptography and the issue of collisions, I posed a question as to whether it was ever possible to store every single possible combination of a bit array of a particular size, in a bit array ...
7 votes
5 answers
4k views

Which programming languages don't use operator precedence besides Lisp-like languages? [closed]

And what do you think about operator precedence? Would be harder programming in a language where the operations are executed in sequential order? Ex.: 2 + 3 * 4 == 20 2 + (3 * 4) == 14 OK, the Lisp ...
2 votes
2 answers
729 views

How to use chebyshev polynomials to calculate exponents (antilogs)?

I am trying to write an algorithm to accurately calculate exponents (antilogs) for a variable precision floating point library I am working on. The base is not relevant since I can convert between ...
4 votes
3 answers
9k views

How does a website like Mathway work?

I recently found a website called Mathway Basically, it works by allowing you to choose your "level of math" (which it uses to determine what tools it should provide to you) and then allows you to ...
22 votes
7 answers
2k views

How can a large, Fortran-based number crunching codebase be modernized?

A friend in academia asked me for advice (I'm a C# business application developer). He has a legacy codebase which he wrote in Fortran in the medical imaging field. It does a huge amount of number ...
-1 votes
5 answers
2k views

Why is What Every Computer Scientist Should Know About Floating-point Arithmetic considered mandatory reading? [closed]

What Every Computer Scientist Should Know About Floating-point Arithmetic is widely considered absolutely mandatory reading for every programmer. Why is this the case? What aspects of the article make ...
1 vote
0 answers
195 views

What algorithm can I use to spread a workload between two processor with fixed resources?

I need to write an algorithm to allocate x number of tasks to 2 processors per day. I know the following: Exact amount of time it will take to complete each task The exact amount of time available ...
7 votes
2 answers
627 views

some misunderstanding in concept of Huffman algorithm

What is difference between Average length of codes and Average length of codewords in Huffman Algorithm? is both the same meaning? I get stuck in some facts: I see a fact that marked as False: for a ...
-1 votes
5 answers
2k views

Why is converting 0-indexed code to 1-indexed code non-trivial?

The disadvantages of 1-indexing are well-known. However, our hand is sometimes forced by our choice of language and we have to convert algorithms that were intended for a 0-indexed language to being 1-...
4 votes
3 answers
680 views

Why do computer scientists seem to avoid closed-form solutions to mathematical problems?

There are many instances of software developmet where we seem to solve problems differently than from a mathematical perspective. For instance, consider the classical example of calculating Fibonacci ...
1 vote
4 answers
1k views

Is using multiple UUIDs decrease chance of collisions exponentially?

For example if you have a single UUID with a collision probability of x, if you concatenate 2 UUIDs, does the collision probability become x^2? val0 = generate_uuid() val1 = generate_uuid() final_val ...
3 votes
1 answer
193 views

Integrating TeX into a Java desktop application

Looking to integrate TeX equations in a TeX-agnostic fashion, suitable for either ConTeXt or LaTeX, into a Java-based desktop Markdown editor. The possibilities are numerous, but I'm not sure what ...
4 votes
2 answers
795 views

How to show fluctuating data smoothly and accurately?

I am working on a project in which I have to read data from liquid level sensor installed in tank which measures fluid level and sends output in millivolt. The device has predefined ranges of voltage ...
36 votes
4 answers
58k views

Prerequisite math skill for Introduction to Algorithms (CLRS) book [closed]

I already have knowledge about basic algorithms. Now I plan to study more advance algorithms and I decide to go with Introduction to Algorithms. I'm not sure, do I need to refresh my math's skill ...
-3 votes
1 answer
60 views

Should absolute value functions be overloaded to give the norm of vector-like data structures?

The absolute value function is an instance of the norm function in one dimension. I've seen instances of libraries where the absolute value function is overloaded to also take the norm of higher ...
-5 votes
1 answer
79 views

What is the difference between BigInt and "Computer Algebra System" integers?

Obviously I imagine a CAS does more than BigInt does. But I'm wondering if there is a difference in implementation between BigInt integers and CAS integers (whatever they may be, symbols?). Wondering ...
0 votes
2 answers
136 views

How to minimize size of data given the need for a null pointer?

So I don't know how to formulate this clearly, as it's quite confusing. Hence the reason for asking this question, to overcome the confusion. I will do my best to make it clear, but part of the ...
1 vote
2 answers
54 views

Suitable way to round results returned by any function of a class

Assume there is a class Shape. The class has two functions area() and perimeter(). Let's say Circle and Square inherit from Shape and override these methods. Obviously the results are going to be ...
3 votes
2 answers
1k views

Algorithm for windowed online covariance

I'm trying to adapt an algorithm to calculate covariance to work over a rolling window on the data. Wikipedia has an algorithm for online covariance: def online_covariance(data1, data2): mean1 = ...
9 votes
3 answers
741 views

What rationale is used when programming language designers decide what sign the result of modulo operation takes?

Going through Modulo operation (the avenue I entered while exploring the difference between rem and mod) I came across: In mathematics the result of the modulo operation is the remainder of the ...
-1 votes
1 answer
55 views

Resolving Dependencies and Incompatibilities Deterministically

Problem Description I'm working in Python and I've been having a problem designing something to handle the following (abstracted) system: I have some objects (lets call them Nodes) that can be in ...
2 votes
3 answers
134 views

Is there a way to specify a two-way relationship between variables?

Let's say we have two variables, eta and phi related by eta = cos(phi). Is there a way to link these variables in any programming language such that there's no need for two different functions, ...
0 votes
6 answers
5k views

Given a number which is power of 2, check if it is even power or odd power

I am given a number n , which is always a power of 2. I want to check whether n is odd power of 2 or even power of 2. For example: n=4 , ans= even n=8, ans=odd (because 8 is 2^3) n=1024 , ans=even. ...
23 votes
11 answers
2k views

How is fundamental mathematics efficiently evaluated by programming languages?

As I get more and more involved with the theory behind programming, I find myself fascinated and dumbfounded by seemingly simple things.. I realize that my understanding of the majority of fundamental ...
62 votes
1 answer
90k views

Treating a 1D data structure as 2D grid

I am working with a native class that represents a 2D image as a 1D array. If you want to change one pixel, for example, you need to now how to derive the index from the x,y coordinates. So, let's ...
2 votes
3 answers
632 views

What effects does memory space have on bitwise shifts?

It is true that the bitwise left shift operation (shl) doubles the value of the integer being shifted. However, when constrained to a finite space, such as 8 bits for example, left shift will begin to ...
10 votes
8 answers
37k views

What is the difference between a floating decimal number and fixed decimal number?

Please explain in layman's terms.
25 votes
4 answers
6k views

Why is the norm of a complex number in the C++ complex library actually the square of the norm?

In the C++ complex library, the method norm() of a complex number actually returns the square of what I have learned is usually called the "Norm". Reference: std::norm For example, std::norm() of (3,...
7 votes
1 answer
602 views

Smallest Rubik's cube state representation

I'm trying to determine what is the fewest number of bits I need to represent the state of a Rubik's cube. (EDIT: I am assuming the cube is a valid Rubik's cube that has not been altered and only ...

1
2 3 4 5 6