For questions with concerns specifically related to the usage of floating point, such as accuracy and precision of calculations, handling of 0, infinity, and over/underflow, input/output, and binary representation. Not for code that casually happens to use floating point.
3
votes
1answer
49 views
Portably generate uniformly random floats from mt19937 output
My goal is to generate a sequence of random float from a seeded random generator. The sequence should be the same on any machine / any compiler -- this rules out ...
3
votes
1answer
49 views
Workaround for the precision limitation for Python's round() function
I'm trying to come up with a general way to correctly round floats with Python, given the known limitation of round():
Note: The behavior of ...
4
votes
0answers
94 views
Linear Interpolation C++
I have to write a collection of methods for performing linear, bilinear and trilinear interpolation. I have also to write some tests to show that interpolation is exact for polynomials (which should ...
5
votes
4answers
895 views
4
votes
1answer
76 views
Preventing numbers from showing up in scientific notation
We have a StreamBuffer class which is not inherited from std::ios_base (or some of it's derivatives such as ...
0
votes
1answer
39 views
Creating indexes from a theoretical decimal/int, splitting into before and after arrays
I'm currently populating before and after arrays with indexes, based on a number provided. If the input going in is an int, ...
3
votes
1answer
39 views
“The Trip” expense-equalizing challenge [closed]
I've solved The trip from Programming Challenges. My solution suffers from lack of floating-point precision, though.
The problem:
Your job is to compute, from a list of expenses, the minimum ...
3
votes
0answers
31 views
Extension of PHP `round()` for PHP_ROUND_UP and PHP_ROUND_DOWN - follow-up
The original question can be found here.
While looking at PHP.net this afternoon, I saw a comment in round() for two functions for rounding ...
1
vote
1answer
35 views
Extension of PHP `round()` for PHP_ROUND_UP and PHP_ROUND_DOWN
While looking at PHP.net this afternoon, I saw a comment in round() for two functions for rounding UP and ...
1
vote
1answer
41 views
Span decimal point precision
In a JSP page I have a number of Span elements. Each of this span element will be floating point numbers.
All these span elements has class ...
10
votes
3answers
88 views
Reinventing the Math Functions
Just for practice in the mathematical side of programming, I decided to rewrite the math functions, with the addition of the root() function, which the Math library ...
14
votes
5answers
2k views
One program using three functions to compare two double-precision numbers
I am currently learning C++ though Bjarne Stroustrup book Programming Principles and Practices using C++.
Going through the drills, I am trying to purposefully write efficient and readable code (good ...
2
votes
1answer
49 views
Create a rotation matrix [closed]
I adapted this piece of code from a math book for game developers and the result is correct albeit with slight floating point errors.
...
3
votes
2answers
180 views
Float to Byte Array Serialization Over Network
I wrote this code for a multiplayer game I am developing to transmit floats over the network. It works, on the systems that I have tested it with. What I am worried about is the little-endian ...
3
votes
1answer
96 views
Finding nearest float values corresponding to a data point (of three variables) in a table with multiple columns using C++
I have a data file which contains floating point data arranged in 4 columns. Each row represent a specific data point. The first column(X) is made up of 100 different values but the values are ...
4
votes
1answer
84 views
Movement code for a game that should be able to handle almost infinite coordinates
Now I've grasped the very basics of Pygame, I thought it'd be useful to make a few classes that I could use later on if I try make a simple RPG style game. I've done this to handle the coordinates for ...
4
votes
3answers
1k views
2
votes
1answer
79 views
A Scala script for beautify scientific notation strings from Excel
Excel renders scientific notation in the engineering fashion, e.g. “4.2E-5”. That’s OK, but when you are writing a scientific paper, it’s likely that you want it to look like this: “4.2×10⁻⁵”.
I have ...
8
votes
3answers
314 views
Floating point equality in Java - follow-up
This is a follow up post for this question.
There aren't a lot of changes. The changes (all of them major) are:
I've decided to ditch my previous approach using ...
21
votes
4answers
806 views
Floating point equality in “Numbers.java”
Floating point inaccuracies are really annoying. I understood that in its true sense while developing the next version of Point (this time I'm actually foolproofing my code). Before I upload it for ...
4
votes
1answer
83 views
5
votes
1answer
165 views
A Range object for Java that partially implements `List`
I'm writing a neural net which uses a genetic algorithm to adjust the weights. To represent the possible range of "genes" for the GA, I'm passing in a list of bases. Unfortunately, I realized after ...
4
votes
2answers
103 views
Get an integer as input and display Pi rounded to that amount of decimal places
I've been writing a program that accepts an integer as input and displays the number Pi rounded to that number. The only issue I see is the fact that the Math.Round ...
3
votes
1answer
53 views
Right-justifying a number with dot leaders
I'm working on a small library to help me format numbers easier as my current project deals with a lot of them. I want to be able to just supply the value, precision, width, and fillchar to a method ...
4
votes
1answer
64 views
Check if a String is a valid Double
For a class, I need to ask the user for a double. To verify that the number can be safely passed to Double.parseDouble, I wrote ...
5
votes
1answer
234 views
JavaScript 8-bit floating point
To start, I am in the wrong language and I think it is time learn some C++ and compile it as an add-on for NodeJS. For now, though, I have a few code snippets that work that may be interesting or may ...
10
votes
3answers
140 views
Distance between two n-dimensional points (NASM)
I just finished writing a function that computes the distance between two n-dimensional points.
The original one was written in C and it's basically a translation of this formula:
...
2
votes
3answers
2k views
Converting a double to a std::string without scientific notation
Problem
I need to convert a double to a std::string which contains no scientific notation and no unnecessary fillers.
Using ...
6
votes
4answers
125 views
Build a double from a stream of chars
Back in my calculator post, janos suggested I either find a better way to read a double from a StreamReader or else go the whole ...
4
votes
2answers
158 views
Converting a number to binary and back as a test in Java
This is one of my first takes on Java, coming mainly from Matlab I found I had many things to learn.
The code will try to convert a number into an array of ones and zeros and then do the opposite ...
4
votes
2answers
164 views
Storing currency-precision values
I'm trying to develop a program that stores currency values. In my particular application I only care about two decimal of precision (cents) but have read it's a good idea (for accuracy when dealing ...
4
votes
2answers
239 views
Java helper function to round a number to the specified number of decimal places
The function I need:
Rounds a double to the specified number of decimal places
I figured this would be a part of java.lang.math, but inexplicably it doesn't ...
5
votes
4answers
2k views
Compare two doubles to N decimal places
I have a function that compares two doubles to N decimal places.
...
4
votes
2answers
1k views
Square Root Calculator
I have now written a simple square root calculator using the division method:
...
8
votes
3answers
278 views
Computing the relative magnitude of the values in one double array to another
An interview question I received:
You are given two unsorted lists of positive doubles
x contains the results from experiment 1
...
19
votes
8answers
2k views
Optimize custom double.parse()
In my company's code, they use double.tryParse() which is quite good but sets too much security for our needs. As we sometimes have to parse a few billion strings, ...
1
vote
2answers
157 views
“Rational” Wrapper class with support for Rational Number Approximation
Enough with imprecise floating point datatypes called float anddouble. I present to you Rational. Of course, Rational number ...
11
votes
2answers
722 views
Foolproof number validation
I know there have been a lot of post, blog article answer on Stack Overflow (but the validation does return true on a tab character) about this. But I'm still ...
10
votes
4answers
1k views
Computing the square root of a 64-bit integer
When sieving primes up to some 64-bit number n I need to determine an upper bound for the greatest potential factor of n, which ...
8
votes
2answers
293 views
Parsing integers safely
Background
I'm writing user input validation for a client-side JavaScript web application. Users enter numerals representing an integer into an <input>. As ...
3
votes
2answers
224 views
Function that normalizes a vector of double
I have written the following function in order to normalize a container of double, so that it sums up to 1.0:
...
6
votes
2answers
433 views
Parse floats from a string
While fixing some old code I came up with a class that parses floats from a string.
This is used for parsing svg files in xml format. Floats can be separated by comma, space or anything at all in ...
6
votes
4answers
1k views
Raising a number to an integral power
I'm currently learning Java along with the Stanford online "Programming Methodology" course and, while it is awesome, I do lack any kind of human feedback.
This exercise was to create a method that ...
19
votes
4answers
2k views
The Term-inator: Pi edition
The fourth project, continuing my C++ saga with terrible post names. :P
An approximate value of pi can be calculated using the series given
below:
$$ \pi ...
0
votes
2answers
212 views
Checking for a double parameter using Big Decimal
I'm seeking a review of correct design practices.
...
7
votes
4answers
1k views
Effectively calculate the result of geometric series
Given \$f(n) = 1 + x + x^2 + x^3 + \ldots + x^n\$ and the fact that computers take more time when multiplying two numbers than when adding, how can we work out the result with greater efficiency?
...
10
votes
2answers
760 views
Fixing double arithmetic errors
I am trying to account for the errors in double arithmetic in a C# program I am writing, and came up with this first solution that seems to work well for most cases:
...
4
votes
3answers
569 views
C++ implementation of Java's floatToIntBits() and intBitsToFloat()
I am trying to implement Java's floatToIntBits() and intBitsToFloat() methods in C++. The latter method is the inverse of the former method, and the purpose of the former one is to pack a 32-bit ...
8
votes
4answers
547 views
Three functions for computing simple arithmetic
Here is my code where I have 3 functions that do simple arithmetic:
...
4
votes
2answers
14k views
SQL percentage calculation
I have created a query in which there is a column that calculates a percentage:
...