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.
1
vote
1answer
35 views
Create a rotation matrix [on hold]
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
59 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
87 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
79 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
937 views
2
votes
1answer
47 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
291 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
728 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
67 views
5
votes
1answer
152 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
95 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
51 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
59 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
181 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
129 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
987 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
120 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
135 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
127 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
192 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
1k views
Compare two doubles to N decimal places
I have a function that compares two doubles to N decimal places.
...
4
votes
2answers
893 views
Square Root Calculator
I have now written a simple square root calculator using the division method:
...
8
votes
3answers
252 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
...
18
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
128 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
707 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
855 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
275 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
177 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
381 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 ...
18
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
168 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
576 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
428 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
543 views
Three functions for computing simple arithmetic
Here is my code where I have 3 functions that do simple arithmetic:
...
4
votes
2answers
10k views
SQL percentage calculation
I have created a query in which there is a column that calculates a percentage:
...
10
votes
1answer
1k views
Kahan summation
Inspired by another question, I decided to implement Kahan summation in C++ (though that question implemented a different summation algorithm).
Since I was writing C++, I decided to make the code ...
8
votes
3answers
390 views
Numerically stable sum of `double`s
It's known that if you naively sum up a collection of arbitrary floating-point numbers using any floating-point standard (e.g. floats or ...
10
votes
6answers
2k views
Divide two numbers, then apply a custom rounding rule
If the fractional part of the number is greater than 0.6, round up; if less, round down.
I need feedback about my code.
...
4
votes
2answers
220 views
Calculator with significant figures
I'm writing a small scientific programming language, and I thought my first step would be to write a calculator with built in significant figures. I wrote this with JParsec for the lexing and parsing. ...
2
votes
1answer
156 views
Implementation of `ulp` and `nextAfter` for platforms where these are missing
I need the ulp method in Codename ONE, and since it isn't available there, I tried to come up with my own version. The code is completely written from scratch, ...
6
votes
3answers
4k views
Truncate decimal places
In one finance application I'm working on, the requirements call that I truncate a decimal value at certain number of places and not round.
Example: 11.685 truncated to 2 decimal places should be ...
4
votes
1answer
77 views
Number formatting
As part of a simple (naive) internationalization attempt in Go, I am trying to come up with a number formatting routine with customizable decimal and thousands separator.
Is this approach alright?
...
1
vote
1answer
97 views
Determine how many bits are in a floating point value [closed]
I'm writing a program which determines how many bits are in a floating point value (this will be run on Unix).
...
10
votes
6answers
2k views
Approximating the square root using an iterative method
I wrote this code, based on the Newton-Raphson method, to find the square root of a number. I'm wondering how I can optimise this code, as I am out of ideas.
...
8
votes
2answers
239 views
Finding the nearest Rational to a double - is there a more efficient mechanism?
I use the following code to find the lowest denominator Rational that is within a certain delta from a double.
The rationale is that the I am pulling float numbers ...
9
votes
1answer
561 views
Generic radix sort
This started out with my answer to Radix Sort on an Array of Strings?. Since I intend to write a generic radix sort for my own purposes anyway, I continued a little bit, and here is a version tested ...
3
votes
2answers
905 views
Half precision reader/writer for C#
I'm reading/writing half precision floating point numbers in C#. These are basically 16 bit floats, compared to the usual 32/64 bit floats and doubles we are used to working with.
I've taken some ...