Should I be using double or float here?

  • Thread starter Thread starter SlurrerOfSpeech
  • Start date Start date
  • Tags Tags
    Float
AI Thread Summary
The discussion centers on the choice between using float and double data types in a C# implementation for a HackerRank problem. The main focus is on precision differences, with double providing about 15 digits of precision compared to float's 7 digits. The conversation highlights that modern programming languages typically promote float inputs to double for mathematical operations, which can lead to precision warnings when casting back to float. While using float may save memory and potentially speed up processing on certain architectures, most contemporary systems, especially x86 CPUs, are optimized for double precision. The conversation also touches on the relevance of these data types in embedded systems, where hardware limitations may dictate the use of float. Overall, the consensus leans towards using double for better precision in general programming scenarios, especially when working with standard libraries and modern processors.
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 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 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
Views
2K
Replies
3
Views
3K
Replies
5
Views
2K
Replies
13
Views
5K
Replies
0
Views
311
Back
Top