double precision error on Mac OSX

This is a discussion on double precision error on Mac OSX within the C Programming forums, part of the General Programming Boards category; Hi users, I am having problems when I want to code in double precision and compile on a Max OSX, ...

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    114

    double precision error on Mac OSX

    Hi users,

    I am having problems when I want to code in double precision and compile on a Max OSX, 10.5, darwin. I compile with gcc-4.0.1

    Would that be a compiler fault on the machine? Is it machine dependent? How can I solve this issue?
    What I obtain is actually a wrong result; I proved it by coding the same software suing floats instead.

    Is anyone a Mac user here and who normally uses doubles instead of floats?

    thank you in advance,
    All the best,
    cfd

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Yes, I use a Mac and use doubles and have not had any problems (not directly attributable to my failure to write working code). Do you have a problem? If so, what is it, actually?

  3. #3
    Registered User
    Join Date
    Mar 2009
    Posts
    114
    hi tabstop,

    This is the problem: when I code my program using floats the software performs correctly, but when I change "float" with "double" everywhere, the code still compiles, but the executable outputs wrong numbers. Very different results from the float case

    thanks for helping

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    "Very different results from the float case" is kind of what you would expect, n'est-ce pas? I presume you're doing a "toy" problem where you know the answers to make sure everything's good. Are you calling other (people's) code? If so, did that change to double too?

  5. #5
    Registered User
    Join Date
    Mar 2009
    Posts
    114
    hi, actually, I would expect different results within certain limits (after a certain floating point); what I am getting is NAN where I am supposed to obtain real value.

    I am not calling functions written elsewhere in this code actually. I need to use doubles for the problem at hand (grid generation).

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    You should show some code. NaN often derives from dividing by zero, but it doesn't have to.

  7. #7
    Registered User
    Join Date
    Mar 2009
    Posts
    114
    Hi again tabstop,

    I would like to show the code, but it's an actual software that I am developing and it has about 15 functions. What I did, I entered eachone of the them and changed the floats into double where needed (including the *.h). In compilation I get no warning or errors and it executes fine. However, when I go and plot my output, that is when I get the unexpected error.

    If it is difficult to show the code and that is the only way to try to solve the problem, I will try again to substitute the floats as little at the time as possible, and compile step by step.

    thanks again for helping. It helped knowing that you usually code in double on your os x and it works fine.

    Best
    cfd

  8. #8
    Registered User
    Join Date
    Mar 2007
    Posts
    142
    If you get totally different results, like NaN maybe you have some issue with declarations. Without some code we can't tell if your functions just return doubles or you pass pointers or maybe you have doubles inside some structs so just make sure all declarations are proper, you have included all required headers and such. Do you compile with default settings (which are useless / dangerous) or do you use something like -Wall as compiler flags?

  9. #9
    Registered User
    Join Date
    Mar 2009
    Posts
    114
    Hi idelovski, thanks for writing,

    SInce it is kind of difficult to send the code or part of it, I can surely send the makefile:

    Code:
    CC = gcc     #Compiler is gcc
    LD = gcc -lm #Linker is gcc also
    
    CFALGS = -Wall -shared
    CDEBUG = -g
    
    #CBLAS = -lgsl -lgslcblas
    
    # Linker flags go here. Currently there aren't any, but if we'll switch to
    # code optimization, we might add "-s" here to strip debug info and symbols.
    LDFLAGS =
    
    # List of source codes and generated object files:
    SRCS = main.c nrutil.c visit_writer.c load_input.c PRINT.c linspace.c nrspline.c define_boundary.c parabola.c gaussj.c interpol_spline.c arco.c TFI.c GRID2CONN.c wrt2plotfile.c external_geometries.c
    OBJS = main.o nrutil.o visit_writer.o load_input.o PRINT.o linspace.o nrspline.o define_boundary.o parabola.o gaussj.o interpol_spline.o arco.o TFI.o GRID2CONN.o wrt2plotfile.o external_geometries.o
    
    # ProgRam executable file name:
    EXE = mymeshGen2D_struct.a
    
    # Top-level rule, to compile everything
    all: $(EXE)
    
    # Rule to Link the program
    $(EXE): $(OBJS)
    	$(LD) -L/usr/local/lib $(OBJS) $(GSLLIB) $(CBLAS) -o $(EXE)
    	
    # Rule for all files:
    $(OBJS): $(SRCS)
    	$(CC) $(CFLAGS) $(CDEBUG) -I/usr/include -c $(SRCS)
    I will be re-checking the arguments and declarations again.

    Thank you

  10. #10
    Epy
    Epy is offline
    Hen-teaser Epy's Avatar
    Join Date
    Sep 2009
    Location
    California, USA
    Posts
    880
    cfdprogrammer, unless you are explicitly prohibited from posting the code, there's not really a reason not to. It's not like someone is going to steal your CFD code and go and write their own from yours...there are already plenty of mature free/open-source CFD programs out there (code_saturne, Elmer, Gerris Flow Solver) that are far more appealing than unfinished code.

  11. #11
    Registered User
    Join Date
    Mar 2009
    Posts
    114
    Hi Epy; it's not a matter of posting an unfinished code for someone to steal it. I have several of my own open source codes already online on my web. The only reason for not posting this one here is that this is not one code; it is a collection of 13 functions for a total of 1000 or so lines, and I doubt that anyone in this forum is willing to read and "debug" since the problem I get depends on each one of these functions.

    Regards
    Last edited by cfdprogrammer; 09-22-2009 at 02:21 PM.

  12. #12
    Registered User
    Join Date
    Sep 2009
    Posts
    6

    Cool Isn't a simple cast?

    Sometimes, when you do a operation with left result as double, and expression
    having a "long", some compilers might require a explicit cast:
    long a;
    double b = a / a;

    Such things may not happen using float as left since it fits the legacies address sizes
    and usually the compiler knows how to handle it.
    float c = a / a; // may not have any problems with that

    so

    double d = (double) a / a; would fix your problem

    Regards,
    yezaim

  13. #13
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,262
    The answer is plain and simple - You have a bug!
    Why? Because the reality is that it's always a bug in someone's code when they think it isn't.

    Now if you're more interested in trying to convince others otherwise without showing any code then you're only serving to boost your own ego.

    On the other hand if you're actually interested in finding the bug (which I'm sure you are), what you need to do is to produce a minimal yet complete program that produces the problem. That means you start with a program that gives you a wrong result, and start taking bits out. Then run that and if the problem is still there take more out and try it again. If the problem disappears, undo your last change and try taking something different out. If you end up doing that last step too many times then you may have found a minimal complete example. Note that this would normally be less than about 30 lines of code.
    Sometimes in the process of doing this you find the bug yourself. Other times you post the remainder here and we tell you where you've goofed up.

    Your alternative is to attach the whole source file to your post and give someone else a thorough enough explanation of what is wrong with the result to solve it themselves. Here's a hint though; you'll find it a lot more valueable to narrow it down yourself.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  14. #14
    Registered User
    Join Date
    Mar 2009
    Posts
    114
    Quote Originally Posted by yezaim View Post
    Sometimes, when you do a operation with left result as double, and expression
    having a "long", some compilers might require a explicit cast:
    long a;
    double b = a / a;

    Such things may not happen using float as left since it fits the legacies address sizes
    and usually the compiler knows how to handle it.
    float c = a / a; // may not have any problems with that

    so

    double d = (double) a / a; would fix your problem

    Regards,
    yezaim
    Hi yezaim,

    thanks for helping. This is the hint I needed. It is a problem in the use of doubles on a machine I barely started to use.

    Thank you again, I appreciate it
    Best

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Testing some code, lots of errors...
    By Sparrowhawk in forum C Programming
    Replies: 48
    Last Post: 12-15-2008, 03:09 AM
  2. Copying 2-d arrays
    By Holtzy in forum C++ Programming
    Replies: 11
    Last Post: 03-14-2008, 03:44 PM
  3. need some help with last part of arrays
    By Lince in forum C Programming
    Replies: 3
    Last Post: 11-18-2006, 08:13 AM
  4. newbie needs help with code
    By compudude86 in forum C Programming
    Replies: 6
    Last Post: 07-23-2006, 08:54 PM
  5. Unknown Math Issues.
    By Sir Andus in forum C++ Programming
    Replies: 1
    Last Post: 03-06-2006, 05:54 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21