Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

C/++/# Should I be using double or float here?

  1. Jul 30, 2016 #1
    Was doing this HackerRank problem and my solution is

    Code (Text):

    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?
     
  2. jcsd
  3. Jul 30, 2016 #2

    phinds

    User Avatar
    Gold Member
    2016 Award

    "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?
     
  4. Jul 30, 2016 #3

    jedishrfu

    Staff: Mentor

    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.
     
  5. Jul 30, 2016 #4

    jim mcnamara

    User Avatar

    Staff: Mentor

    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.
     
  6. Jul 31, 2016 #5
    Ah, now I see.
     
  7. Jul 31, 2016 #6

    rbelli1

    User Avatar
    Gold Member

    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
     
  8. Jul 31, 2016 #7

    jedishrfu

    Staff: Mentor

    This is true and is a good point. However, i was referring to modern programming languages not low level CPU architectures.
     
  9. Jul 31, 2016 #8

    jim mcnamara

    User Avatar

    Staff: Mentor

    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: Jul 31, 2016
  10. Jul 31, 2016 #9

    rbelli1

    User Avatar
    Gold Member

    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
     
  11. Aug 1, 2016 #10

    jim mcnamara

    User Avatar

    Staff: Mentor

    @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.
     
  12. Aug 1, 2016 #11

    rbelli1

    User Avatar
    Gold Member

    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
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?
Draft saved Draft deleted



Similar Discussions: Should I be using double or float here?
  1. Using float in C (Replies: 3)

Loading...