Thread: Replace double with long double in all my code.

  1. #1
    Registered User
    Join Date
    Jul 2005
    Posts
    31

    Question Replace double with long double in all my code.

    Hello,

    I have a quite a lot of code, 30 files or so, most of which is mathematical number crunching stuff. All the floating point number are defined as the double data-type. Is there a quick and easy way to replace all the double types with long double types; maybe using a pre-compiler command? The reason being, I would like to run my application with a higher level of precision.

    Any ideas on that?

    Cheers,

    Dan.
    Last edited by boyfarrell; 04-30-2007 at 03:25 PM.

  2. #2
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by boyfarrell View Post
    I have a far amount of code, 30 files or so, which is mostly mathematical number crunching stuff. All the floating point number are defined as double. It there a quick and easy way to, may be using a pre-compiler command to replace all my double with long doubles as I would like to run my application with a higher level of precision?
    You could #define "double" to "long double," but that's a bad solution because it means you can't use plain old "double" anywhere, even if you wanted to.

    You should just bite the bullet and change all the occurrences of double to "real_type" and then do this in a header file somewhere:

    Code:
    typedef long double real_type;
    Then, in the future when you want to use "long long long long double" or something, you just change this typedef.

  3. #3
    Registered User
    Join Date
    Jul 2005
    Posts
    31
    Hi,

    I tried using #define however it does seem to work in my simple example programme,

    Code:
    #import <stdio.h>
    #define double long double
    
    int main(int argc, char *argv[])
    {
      unsigned doubleBytes = sizeof(double);
      printf("\ndouble bytes =  %u",doubleBytes);
      unsigned longDoubleBytes = sizeof(long double);
      printf("\nlong double bytes =  %u",longDoubleBytes);
       
      return 0;
    }
    It seems that I will have to find/replace everything. This is a shame as I will have to keep track of two code bases which do the same thing! Maybe I can make some sort of script to do the changing from double to long double?

  4. #4
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Code:
    #import <stdio.h>
    #define double long double
    I think you mean #include there. #import, if it does anything, doesn't do what #include does and is a non-standard extention of your compiler. This could explain why you are getting errors.

    #define double long double is also just a really bad idea because it will do that text substitution for the whole file and whatever files it includes. I woud just use grep or something like that. If you don't have that tool, than just use the find-replace option in your editor. I doubt it needs to be more complicated than that. brewbruck's typedef idea is worth implementing as well.

  5. #5
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by boyfarrell View Post
    unsigned longDoubleBytes = sizeof(long double);
    Since you have redefined "double" to "long double," this line of code changes into "long long double" which is a non-existent type. Thus my admonition to NOT use this technique.

    It seems that I will have to find/replace everything.
    Shoulda been using a typedef since the beginning. Now you have no choice.

    Maybe I can make some sort of script to do the changing from double to long double?
    Maybe, but the possibility of screwing up is high. For instance, if the code already reads "long double," it should not change the word "double" to "long double." And if the word "double" appears in double quotes, it probably shouldn't change. And if it occurs in a comment, should it change? Only a human can decide that.

    Just do a query find replace and sit there for three hours making sure only the appropriate stuff gets changed. Then remember never to make this mistake again.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    1. Use a typedef in preference to a #define

    2. Any decent code editor should be able to perform a global context replacement for you without too much difficulty.

    > The reason being, I would like to run my application with a higher level of precision.
    IIRC, long double only adds 3 decimal digits (18 rather than 15), so what are you really getting for your effort?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Math wizard
    Join Date
    Dec 2006
    Location
    USA
    Posts
    582
    Can't you copy your entire code into Wordpad and use the replace feature? The best way is to have a space or other unusual character to help minimize potential replacement flaws. In short, do this:

    1. Open Wordpad and go to file > new choosing "text document" in the list. Notepad may work if it has the "replace" feature (Windows 98, for sure, doesn't but XP's does).
    2. Copy your code you want to replace (or at least the main area where variables are defined).
    3. Paste the code into Wordpad.
    4. Go to edit > replace.
    5. In the "find" find, put "double ". Note the deliberately added space at the end of "double". This helps minimize replacement flaws.
    6. In the "replace with" area, put "long double ", again with the extra space (forgetting it when included before will cause a space to be lost.
    7. Click "replace all" and wait.
    8. When done, check over the output code and if you like it, copy it and paste it into the area you had highlighted in your code editor. This will replace your code with what your intent is.

    I've had to replace huge chunks of code (literally 1000+ lines) with this due to a change in plans and using Wordpad is the fastest, most reliable way of doing so.

  8. #8
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by boyfarrell View Post
    I have a quite a lot of code, 30 files or so, most of which is mathematical number crunching stuff. All the floating point number are defined as the double data-type. Is there a quick and easy way to replace all the double types with long double types; maybe using a pre-compiler command? The reason being, I would like to run my application with a higher level of precision.

    Any ideas on that?
    Before you go about changing stuff, are you using the Microsoft runtime for Win32? Making the change to long double there may effectively do nothing.
    http://msdn2.microsoft.com/en-us/lib...15(vs.80).aspx
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  9. #9
    Registered User
    Join Date
    Jul 2005
    Posts
    31
    Hi,

    Code:
    #import
    is apart of the Objective-C run-time. It is the same as
    Code:
    #include
    , however, it first checks to see if a file has already been included, if so I won't include it again.

    The reason for the change to long double is to increase range of floating point numbers when the code is run on a Intel (Linux) processor as compared to a PowerPC (Mac). It should be about ~3000 orders of magnitude!

    Think I will go with the global find and replace option.

    Thanks for you help.

    Dan.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 03:07 AM
  2. newbie needs help with code
    By compudude86 in forum C Programming
    Replies: 6
    Last Post: 07-23-2006, 08:54 PM
  3. converting double to long int
    By gunshipolitico in forum C++ Programming
    Replies: 5
    Last Post: 11-07-2005, 10:26 AM
  4. How to display 23 bytes long double answer in C?
    By CLIE_ZETA in forum C Programming
    Replies: 3
    Last Post: 11-18-2001, 12:11 PM
  5. Hi, could someone help me with arrays?
    By goodn in forum C Programming
    Replies: 20
    Last Post: 10-18-2001, 09:48 AM