Should I be using double or float here?

  • Thread starter SlurrerOfSpeech
  • Start date
  • Tags
    Float
In summary: I'll just use floats" philosophy seems to be catching on.In summary, using floats may increase speed when dealing with data with few digits of precision.
  • #1
SlurrerOfSpeech
141
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
  • #2
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
  • #3
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.
 
  • #4
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.
 
  • #5
Ah, now I see.
 
  • #6
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
  • #7
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.
 
  • #8
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:
  • #9
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
 

FAQ: Should I be using double or float here?

Should I always use double instead of float?

No, it depends on the data you are working with and the level of precision you need. Double has a higher precision than float, but it also takes up more memory. If your data requires a high degree of accuracy (e.g. in scientific calculations), then double may be the better choice. However, for general use, float is sufficient and more efficient.

What is the difference between double and float?

The main difference is in their precision. Double is a 64-bit floating point number, while float is a 32-bit floating point number. This means double can represent larger and more precise numbers than float. However, float is faster and takes up less memory than double.

Can I mix double and float in my code?

Yes, you can use both double and float in your code. However, when performing calculations, the data types should match. Mixing double and float can result in unexpected results or errors.

When should I use float instead of double?

You should use float when working with large arrays of floating point numbers, as it takes up less memory than double. Float is also faster than double, so if speed is a priority, then float may be the better choice.

Are there any potential errors when using double or float?

Yes, there are some potential errors to be aware of when using double or float. One common error is precision loss, where the value of a number may change slightly due to the limited precision of the data type. Another error is overflow, which occurs when a number is too large to be represented in the data type. It is important to consider these potential errors when deciding which data type to use.

Similar threads

Replies
9
Views
2K
Replies
3
Views
3K
Replies
3
Views
1K
Replies
5
Views
2K
Replies
13
Views
4K
Replies
2
Views
2K
Back
Top