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.
0
votes
2answers
45 views
Parsing a string into a double
I'm trying to understand the strtod() function, and how I can handle any user input given to the function. I assume I tested for everything, however I am hoping for ...
1
vote
1answer
15 views
Solving quadratic equation in Clojure
I wrote a function in Clojure to solve quadratic equations using the quadratic formula
The function
...
3
votes
1answer
75 views
Swift FloatingPoint rounded to places
I'm playing a bit with extensions for default protocols in Swift 3.0.
I would like to implement rounded(toPlaces places: Int) -> Self for ...
2
votes
3answers
132 views
BigDecimal class with arbitrary precision
I recently came across a need to display very large decimal numbers and realized that I could use a BigDecimal class. After some tinkering I decided to leverage the ...
1
vote
3answers
67 views
Haskell function to format a floating-point number with some precision
I have not yet developed a satisfying coding style in Haskell. In the code snippet below, there is a lot wrong to my taste:
Helper functions which should be local to ...
0
votes
4answers
95 views
1
vote
1answer
49 views
Make sure the user enters a valid floating-point number in C
I would like to know if this is a good way to make sure the user enters a valid number.
...
2
votes
2answers
185 views
Addition of two IEEE754-32bit single precision floating point numbers in C++
I made a C++ program which takes a list of triplets with S.No. from a text file, where the triplet, i.e. a,b,c correspond to a+b=c (addition done using float data type). Now I convert a and b from hex ...
5
votes
2answers
101 views
Atomic floating-point addition
I need to be able to atomically add a 32-bit floating point value to a location in memory. Here is what I came up with. While the code is Windows-specific, I'll extend it with Linux support using <...
1
vote
3answers
68 views
Functor to compare two floats with tolerance
What do you think about this implementation of 2 floats comparison functor considering how tolerance is introduced?
...
4
votes
1answer
73 views
Normalize integer types to float range
I wrote two template functions to take an integer type and output a float in the given range. One function takes signed integers and the other takes unsigned ...
-3
votes
1answer
54 views
Printing a prime normalized sequence
The program below generates a prime normalised sequence. So, what happens is this:
It asks you to provide a number within a range. Program then calculates a factor which is inverse of product of all ...
2
votes
0answers
51 views
Numerical differentiation using the y-intercept
I have a routine for determining the derivative of a function using the y-intercept (B) to infer the finite difference step (h). ...
1
vote
3answers
59 views
Byte array to floating point such as FPE2 in Java
Apple's System Management Controller uses FPE2 and SP78 as floating-point formats.
While writing a Java library that that interfaces with the SMC, I ported someone's C function to convert read a FPE2-...
4
votes
2answers
63 views
Function that strips trailing '0's from a float
I'm writing an embedded application in C, and at one point I need to convert a float into an ASCII representation of that float, so I can send the ASCII over a serial port. The protocol the serial ...
6
votes
1answer
131 views
Generic and accurate floating point “equality”
Like those who have come before me, I foolishly have sought to implement a generic, efficient, idiomatic, and most importantly correct method of comparing floating point numbers for equality. Like ...
4
votes
2answers
119 views
Calculating the equivalent value of 2 resistors or capacitors
I started learning C# and this is my first "project". Its main purpose is to calculate the equivalent value of two resistors or capacitors connected in either series or parallel.
I just finished it ...
1
vote
1answer
61 views
Program to count number of victories in a pool of contests
I changed the program to use a dynamic array instead of a vector or storing the integers. This program works without bugs and executes relatively fast. The only doubt Istill have is how to fill the ...
3
votes
2answers
68 views
3
votes
1answer
91 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
86 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
222 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
1k views
4
votes
1answer
80 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
44 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
51 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
44 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
61 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
50 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
96 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 ...
3
votes
2answers
553 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 big-...
3
votes
1answer
107 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 ...
3
votes
1answer
89 views
Printing doubles using string manipulation
Having mostly used Qt and its classes like QString, I wrote a little exercise with plain C++11. I'm looking for two kinds of input: generally improving the code and/...
4
votes
1answer
89 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
185 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
363 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 ...
22
votes
4answers
900 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
102 views
5
votes
1answer
183 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
122 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
57 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
72 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
288 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
150 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:
\$\text{dist}(x,y)=...
2
votes
3answers
4k 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
128 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
203 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
220 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 ...