Skip to main content

Questions tagged [floating-point]

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.

Filter by
Sorted by
Tagged with
5 votes
2 answers
79 views

TypeScript Number Set

I originally premiered this code to the internet in this StackOverflow post, and per a recommendation through one of the comments, I am reposting it here. I am making a floating-point number set ...
Wasabi Thumbs's user avatar
6 votes
2 answers
423 views

Demonstrate effects of summation order

When we add lots of floating-point values, we can lose precision if we blindly use std::accumulate() or similar, when the running total becomes much larger than the ...
Toby Speight's user avatar
  • 77.3k
3 votes
1 answer
212 views

Calculate the maximum number of decimal places with which all floating point numbers can be represented in a range in C++

I'm looking for a function in C++ that can determine, how accurate all floating point numbers in a given range can be represented as strings, without the use of e.g. boost library, etc. Please take a ...
Tobias Grothe's user avatar
5 votes
2 answers
321 views

An is_integer Template Function Implementation in C++

I am trying to make an is_integer template function to determine a number is an integer or not. The experimental implementation ...
JimmyHu's user avatar
  • 5,390
4 votes
3 answers
3k views

A function to convert 0-2000 milliseconds to a double representing a fraction of a second

...
my_stack_exchange_account's user avatar
3 votes
2 answers
98 views

Solving round number in JS by using String

Whatever language you choose, you may encounter a rounding problem. In fact, this is due to the limit of the required number of bits needed to get the right number after a calculation. Simple example: ...
Sébastien Josserand's user avatar
4 votes
2 answers
136 views

Processing a very long single line of comma-separated (?) floating point numbers

I have a sample svg file from the graphics program Inkscape. My objective is to collect every third pair from a very long line of floating point numbers. That seems simple enough. The larger objective ...
Bryan-StackExchange's user avatar
9 votes
3 answers
3k views

Floating point approximately equal

I implemented the following code for determining if two floating point numbers are equal is_float_equal(...). It handles the tests for my use cases well, but I was ...
dvnlo's user avatar
  • 155
3 votes
3 answers
451 views

Input validation of a signed double (in C++)

I am a beginner, who is just learning the language (C++) from C++ Primer. I have been trying to build a side pico-project by writing functions for validation of user input for an signed double. The ...
SogaBan's user avatar
  • 53
2 votes
1 answer
1k views

C++ compare floats

I have designed a class to perform float compare. I used Knuth's strong compare to do it. Float compare is really really tricky, I'm sure there's something wrong :) Any comments? ...
greywolf82's user avatar
8 votes
1 answer
150 views

Converting a number to a string with a maximum of 3 numerals plus SI unit

I wrote a function that receives a number as an argument and returns a number (technically, a string) with a maximum of 3 numerals, no matter if there's a decimal separator or not, plus the adequate ...
Megaptera novaeangliae's user avatar
1 vote
1 answer
196 views

Conversion from string to long double

This is a follow up to https://codereview.stackexchange.com/questions/272333/conversion-from-long-double-to-string where I do the reverse conversion. This time around it is string to long double. It ...
user253344's user avatar
7 votes
3 answers
1k views

Checking for NaN bit patterns in C++

I live in hostile environments (GCC and Clang) which remove calls to isnan which prevents me to perform validation of my input before running computations. Here is ...
Jean-Michaël Celerier's user avatar
0 votes
1 answer
70 views

Generating floats from integers

I wanted to make an algorithm that is a counter-example of Cantor's diagonalization argument. Given an integer, this Python code will produce a unique rational number. Fed the sequence of positive and ...
Brent's user avatar
  • 451
1 vote
1 answer
168 views

Floating point addition algorithm

I made a function in Rust to add two floating point numbers (f32s) together using only their bit representation and integer operations. I have tested it for a quite ...
isaacholt100's user avatar
0 votes
3 answers
100 views

Convert a fraction into a whole number and a binary exponent (eg 3.125 => 25, 3)

I have a function which takes one argument, a fraction, and returns two numbers - an integer and a power of two. So, when you divide the the integer by 2 raised to power power of two, you get the ...
Krokodil's user avatar
  • 113
2 votes
3 answers
268 views

string to double

I'm trying to write a function to replace the standard std::stod(...). I want to parse the following valid inputs: ...
Cristian's user avatar
  • 147
3 votes
1 answer
606 views

Rigorously checking whether 2 floating point numbers are close in VBA

latest updated version in cross-post from SO I'm testing performance regression of some code I wrote (this is not that code) by timing its execution in Unit tests. I would like to see if execution ...
Greedo's user avatar
  • 2,395
6 votes
2 answers
183 views

An IEEE half-float implementation in python similar to array.array, is there any way I could make this more efficient?

I've written this class to wrap a collection of bytes and interpret them as 16-bit floats. It's supposed to work like memoryview(buf).cast('f') or ...
Sekenre's user avatar
  • 163
2 votes
1 answer
516 views

Safe runtime numeric casts

The rationale behind this code is to implement a runtime-safe number conversions in situations when precision loss is possible, but is not expected. Example: passing a ...
Dmitry's user avatar
  • 121
1 vote
1 answer
3k views

Float to binary conversion

I wrote a program to convert float to it's binary IEEE754 representation in C. Any improvements to this is appreciated ...
C--'s user avatar
  • 113
4 votes
1 answer
242 views

Locale-independent float print function

The objective was to write a locale-independent function that prints floating-point numbers (i.e. one that always uses . as a decimal point regardless of the locale)...
Kirby Kevinson's user avatar
1 vote
1 answer
578 views

GLSL atomic float add for architectures with out atomic float add

...
Krupip's user avatar
  • 641
5 votes
1 answer
194 views

Floating-point to String Conversion with Given Precision for Fractional Part

Faced with converting floating-point values from a sensor obtained to string on an embedded system to transmit over UART, I came up with the following dbl2str() to ...
David C. Rankin's user avatar
7 votes
2 answers
338 views

Accounting for floating point error when rounding

I have to implement some business logic that will round a number (typically a double) upwards. Given that this number comes from a third-party and I don't know how they come to calculate it I'm ...
HexedAgain's user avatar
8 votes
2 answers
472 views

Pretty-print dollar amounts using custom precision for large amounts

Given a floating-point number, which is representing a dollar value, I'm aiming to return a rounded, human-readable string. Here are the rules for numbers equal to or greater than 1 million, equal to ...
Martin Burch's user avatar
2 votes
1 answer
1k views

C# calculate total time worked with TimeSpan [closed]

So there is an existing codebase which calculates the total time worked per month. What the code is doing: iterates through all the working days entries retreives the value of net work time which is ...
tinamou's user avatar
  • 123
3 votes
1 answer
1k views

Plotting the integer values in text format in matplotlib piechart

I have done a clustering algorithm and represented the results in a pie chart as shown below. ...
P H's user avatar
  • 133
2 votes
0 answers
60 views

Suggestions on how to update equation?

After discovering Float Toy, my goal was to create my own version using Python and Tkinter. The results of the endeavor is shown below. Most of the functionality is complete, but the ...
Noctis Skytower's user avatar
2 votes
1 answer
85 views

A program that reads 8763x2 values from a CSV file and initialises an array and few other variables

Relatively new to Python. Have some experience in C++. I have written a small program that reads a CSV file (SAMPLE) and initialises two arrays and few values. ...
Aditya Soni's user avatar
0 votes
1 answer
151 views

Function for comparing floating point numbers

I am trying to make a function for floating point numbers comparison. The goal is to "evaluate" all operators in single function: >, >=, =, <=, <. If X>Y, then F>0. If X==Y, F==0. If X<=Y, F&...
herhor67's user avatar
24 votes
4 answers
4k views

IEEE 754 square root with Newton-Raphson

I have an implementation of the sqrt function that uses a combination of IEEE 754, packed bitfields, and the Newton-Raphson algorithm: ...
S.S. Anne's user avatar
  • 1,725
7 votes
2 answers
364 views

Software floating-point multiplication

I wrote a floating-point multiplication function as an excercise. The program compares its result to the usual hardware multiplication result and for this purpose I use unspecified behavior, but the ...
Nikita's user avatar
  • 173
0 votes
2 answers
171 views

Union of two double arrays

I would like to merge two double arrays and remove from the result array approximately equal items. My two input arrays have ...
Wollmich's user avatar
  • 125
4 votes
1 answer
4k views

Fixed-Size Floating-Point Types

I have created my own personal data structure libraries for C and I've re-engineered it about two times now and I've added alot of preprocessor checks and defines concerning OS type and compiler type. ...
Nergal's user avatar
  • 513
3 votes
1 answer
781 views

Python maintain precision for currency

Working with currencies I am aware it is not good to work with floating point values due to rounding and maintaining precision. Better is to work with integers. Would the following Currency class make ...
Bruno Vermeulen's user avatar
4 votes
1 answer
499 views

Java Mandelbrot Set Viewer

I'm writing my own version of a Mandelbrot set generator, with pan and zoom functionality. (There's not yet a color overlay of sets generated with various beta values; the beta value is changed ...
Pull Nointer's user avatar
6 votes
3 answers
2k views

Function to extract float from different price patterns

I've got a project where I get product data and need to get the prices which come in, in different formats. Some examples would be: US$17, USD17.00, 17,00€, 17€, GBP17, Only 17,-€, 17.000,00€, 17,000....
Chris's user avatar
  • 161
4 votes
1 answer
180 views

Preperiodic, periodic or aperiodic binary string

This is extension of the rep-string task from Rosette code. I do not only want to check if the input string is the shortest periodic string (rep-string) or not, but also check if it is preperiodic. ...
Adam's user avatar
  • 175
0 votes
1 answer
1k views

Converting Array of `UINT8` (`unsigned char`) to Array of `Float32` (`float`) Using AVX2

Given input array of UINT8 (unsigned char) with numElements how could one efficiently ...
Royi's user avatar
  • 582
5 votes
2 answers
524 views

Calculating the number of coins in money change

I wrote a program that calculates the minimum number of coins required to give a user change. One concern I have is: When do we initialize the value of a float to be negative or positive? I recently ...
asimichroma's user avatar
3 votes
2 answers
955 views

Adding thousand separator to various number types

Am making a pure .net library with helper functions (GitHub). However I wanted to have a thousand separator for all number types and here is what I am currently doing ...
Billy Watsy's user avatar
12 votes
1 answer
1k views

Function to print a double, in decimal - exactly

Rarely is the exact decimal value of a double needed to be printed and only its leading significant digits, after rounding, are needed. It is a curiosity to see the ...
chux - Reinstate Monica's user avatar
2 votes
2 answers
480 views

Print a double as a decimal with a specified precision

How could I make this code more effective? ...
ken's user avatar
  • 97
1 vote
2 answers
42 views

Bad performance bash, changing display brightness

I have a script that changes the display brightness using xrandr. It is my first bash script. I mainly had a lot of trouble with dealing with the floating point ...
Tristan's user avatar
  • 183
3 votes
0 answers
99 views

Variable bit length lossy floating point compression

I am implementing a new compression algorithm for the weights of a neural network for the Leela Chess project. the weights are roughly 100Mb of ...
Oscar Smith's user avatar
  • 3,597
6 votes
3 answers
2k views

Custom double parser optimized for performance

I'm trying to beat the native Double.TryParse for performance in parsing large multi-million row (simple) CSV files as much as possible. I do not have to support ...
Alain's user avatar
  • 472
3 votes
1 answer
2k views

Convert string to double and check for errors

The following program converts a string to a double, and checks for errors. It uses the strtod() function to do the conversion, and follows the example given for ...
Domenic's user avatar
  • 31
7 votes
3 answers
751 views

std::isnormal in C#

Is this correct? Is this portable to .NET core and 32-bit ARM Linux? Is there a faster way, maybe something in the .NET framework? Is there a way to implement float32 version without using dynamic ...
Soonts's user avatar
  • 253
1 vote
1 answer
158 views

Scaling decimal numbers up within bounds of limit

This code is part of a simulation and inputs will be positive values less than one and inputs are constrained elsewhere. The goal of the algorithm is to scale the inputs up to much larger numbers, ...
user avatar