Should I be using double or float here?

  • Context:
  • Thread starter Thread starter SlurrerOfSpeech
  • Start date Start date
  • Tags Tags
    Float
Click For Summary

Discussion Overview

The discussion revolves around the choice between using float or double data types in a C# implementation for a HackerRank problem. Participants explore the implications of precision, performance, and the context in which these data types are used, including considerations for embedded systems and modern programming environments.

Discussion Character

  • Debate/contested
  • Technical explanation
  • Conceptual clarification

Main Points Raised

  • Some participants question the understanding of float and double, particularly why one might be more precise than the other in specific cases.
  • One participant notes that using float can reduce disk usage and improve I/O speed, as it requires less storage compared to double.
  • Another participant highlights the precision limits of float and double, citing that double typically has about 15 digits of precision while float has about 7.
  • There is a discussion about the performance implications of using float versus double on different architectures, particularly in embedded systems where hardware support for double may be lacking.
  • Some participants mention that modern programming languages often promote float to double in mathematical operations, which may lead to precision warnings when converting back to float.
  • Concerns are raised about the generalization of performance benefits in modern processors, with some participants emphasizing that not all modern systems have robust floating-point capabilities.
  • One participant points out that the original poster (OP) is using an x86 CPU and C#, and that the discussion should focus on the precision limits of float and double rather than broader floating-point issues.

Areas of Agreement / Disagreement

Participants express differing views on the implications of using float versus double, particularly regarding precision and performance. There is no consensus on which data type is definitively better for the OP's specific use case.

Contextual Notes

Limitations in the discussion include varying assumptions about the hardware and software environments, as well as the specific requirements of the problem being addressed. The conversation also touches on the nuances of floating-point arithmetic and its implications in different contexts.

SlurrerOfSpeech
Messages
141
Reaction score
11
Was doing this HackerRank problem and my solution is

Code:
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;

class Solution
{
    static void Main(String[] args)
    { 
        Console.ReadLine(); // skip first line, don't need it in C# implementation
        int[] nums = Array.ConvertAll(Console.ReadLine().Split(' '), Int32.Parse);
      
        // helper function
        Func<int[], Func<int,bool>, double> ProportionSatisfyingCondition =
            (arr, unpred) => (double)arr.Where(unpred).Count() / (double)arr.Length;
      
        // compute and print proportions
        Console.WriteLine(ProportionSatisfyingCondition(nums, i => i > 0));
        Console.WriteLine(ProportionSatisfyingCondition(nums, i => i < 0));
        Console.WriteLine(ProportionSatisfyingCondition(nums, i => i == 0));
    }
}

but I'm wondering whether I should be using float instead of double. Which is more precise in this case?
 
Technology news on Phys.org
SlurrerOfSpeech said:
but I'm wondering whether I should be using float instead of double. Which is more precise in this case?
"In this case" ? What is your understanding of float and double and why would "this case" cause one to be more precise than the other if it isn't already?
 
  • Like
Likes   Reactions: SlurrerOfSpeech
The usual distinction between double and float mostly related because you store two floats for every double. Basically you decide to give up precision for handling large data. This is especially true when placing data in files using floats cuts disk usage and speeds disk io up since youre reading and writing half as much data.

All math routines in modern languages are based on double inputs and outputs with float input being promoted to doubles prior to the method call and with precision warnings given when you cast the double output back to float.
 
Limits of precision --
DBL_DIGITS (usually defined as 15) is the number of digits of precision for a double datatype.
FLT_DIGITS (usually defined as 7) is the number of digits of precision in a float datatype.
So which do you see has the greatest precision?

Your data has how many digits of precision? Pick the correct one to match your dataset.
 
Ah, now I see.
 
jedishrfu said:
All math routines in modern languages are based on double inputs and outputs with float input being promoted to doubles prior to the method call and with precision warnings given when you cast the double output back to float.

This may be true for architectures with hardware double sized floating point built into the CPU. There are smaller CPU's that have only single precision or no floating point at all. In these cases using float over double may increase speed tremendously. Some don't support double at all and simply use it as an alias for float.

You will generally only find this in embedded systems today. For example most of the Arduino boards have software floating point.

BoB
 
  • Like
Likes   Reactions: jedishrfu
rbelli1 said:
This may be true for architectures with hardware double sized floating point built into the CPU. There are smaller CPU's that have only single precision or no floating point at all. In these cases using float over double may increase speed tremendously. Some don't support double at all and simply use it as an alias for float.

You will generally only find this in embedded systems today. For example most of the Arduino boards have software floating point.

BoB
This is true and is a good point. However, i was referring to modern programming languages not low level CPU architectures.
 
rbelli1 said:
For example most of the Arduino boards have software floating point.

FWIW Intel has a proprietary (free to use) floating point library that works on x86 which already has FP registers. Point here is to handle the issues associated with imprecise representations of decimal numbers in binary format floats and double. Numeric analysis. Example: equality of two variables.

Edit: new Fujitsu cpu chips for new Oracle Solaris boxes have a similar library on the chip, in the same vein as the math coprocessor for pentium. Except it meets the new ieee-2008 754 standard.

https://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html
 
Last edited:
All the floating point libraries in the world will not increase the speed of software calculation. My point was that "modern" does not always indicate 64bit multi gigahertz processors with 80bit hardware floating point and SIMD instructions. To say nothing of multi-threading an multiple cores.

Some very modern processors will have no floating point and some have no hardware multiply at all. The cortex M0+ processors have no floating point an they were released only 4 years ago. Every year Atmel and others release 8 bit processors that have neither FP or hardware multiply.

Little old Arduino and MSP430 and PSOC and PIC and tiny ARM boards are becoming rather popular these days. The whole "screw it we have infinite resources" cheat is not exactly universal.

BoB
 
  • #10
@rbelli1 - Yes, I know that libraries do not speed up processing. And it is a valuable point. But you missed what I was attempting to say: The point I thought I was making is that floating point operations as currently implemented on x86 chips have a raft of issues. CUDA and gpu use of FP routines get around this, for example. But this is going way past the topic at hand:

As far as I can see:
OP is not using Arduino, not using CUDA, not using an FP library. The OP is using an x86 cpu and apparently using C#. OP asked about limits of precision of specifically: float and double precision datatypes. So, other than you and I not communicating with each other, how are we helping the OP?

He did not ask about the C# Decimal datatype, which is currently a library implementation only. AFAIK.
 
  • #11
I guess we have gotten a bit off topic. I just wanted to point out that jedishrfu's answer was valid only in a limited domain.

BoB
 

Similar threads

  • · Replies 9 ·
Replies
9
Views
3K
  • · Replies 2 ·
Replies
2
Views
1K
  • · Replies 9 ·
Replies
9
Views
2K
  • · Replies 3 ·
Replies
3
Views
3K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 11 ·
Replies
11
Views
2K
  • · Replies 13 ·
Replies
13
Views
5K
  • · Replies 1 ·
Replies
1
Views
1K
  • · Replies 2 ·
Replies
2
Views
2K