• C

float.c to test 32 versus 64 bit precision in C

QUESTION:
Shouldn't the floating point precision improve by using a 64-bit compile?
If it doesn't, why not?
Is there a different float.h that I should be using when I compile it in x64?

EQUIPMENT:
Windows 7-x64
Visual Studio 2008 with 64-bit compiler.

BACKGROUND:
I'm using a very simple program to demonstrate floating point limits.  I've compiled it (both ways) within VS2008, and (both ways) with the command line using batch file listed below:  

The output of the program listed below for a 32-bit (x86) compile is as follows:
radix: 2
float: 24 radix digits
      [1.17549e-038, 3.40282e+038]
double: 53 radix digits
      [2.22507e-308, 1.79769e+308]
long double: 53 radix digits
      [2.22507e-308, 1.79769e+308]

I recompile it as a 64-bit, BUT I get the exact same results.  I was expecting an improvement.  

THE PROGRAM---------------------------------------------------
/* float.c: Illustrates floating-pt. limits */
#include <stdio.h>
#include <float.h>

int main()
{
    printf("radix: %d\n", FLT_RADIX);
    printf("float: %d radix digits\n",FLT_MANT_DIG);
    printf("\t[%g, %g]\n", FLT_MIN, FLT_MAX);
    printf("double: %d radix digits\n",DBL_MANT_DIG);
    printf("\t[%g, %g]\n", DBL_MIN, DBL_MAX);
    printf("long double: %d radix digits\n",LDBL_MANT_DIG);
    printf("\t[%Lg, %Lg]\n", LDBL_MIN, LDBL_MAX);
    return 0;
}

BATCH FILE WRITTEN TO COMPILE
(to show you that I'm invoking different compilers
for each case)
---------------------------------------------------------
@echo off

REM CL [option...] file... [option | file]... [lib...] [@command-file] [/link link-opt...]
REM enter your file to compile here:
@SET FILENAME=float

@SET VC64DIR=s:\Program Files (x86)\Microsoft Visual Studio 9.0\VC\bin\amd64
@SET VCX86DIR=S:\Program Files (x86)\Microsoft Visual Studio 9.0\Common7\Tools
@SET WORKINGDIR=V:\MYWORK\A

cd "%WORKINGDIR%"
mkdir %FILENAME%_amd64
mkdir %FILENAME%_x86

REM  compile the 64 bit version, and put it in it's own folder
cd %FILENAME%_amd64
CALL "%VC64DIR%"\vcvarsamd64.bat
cl /EHsc %WORKINGDIR%\%FILENAME%.c

REM compile the x86 version and put it in it's own folder
cd %WORKINGDIR%\%FILENAME%_x86
CALL "%VCX86DIR%"\vsvars32.bat
cl /EHsc %WORKINGDIR%\%FILENAME%.c

REM run the program and sent it to out.txt
V:\MYWORK\A\%FILENAME%_amd64\%FILENAME%>out.txt
V:\MYWORK\A\%FILENAME%_x86\%FILENAME%>out.txt
pause
LVL 1
Darrell ThomasEngineerAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Infinity08Commented:
>> Shouldn't the floating point precision improve by using a 64-bit compile?

It would only improve if the number of bits used for a floating point value is increased.

Generally, the widths of the floating point types are independent of whether the OS/hardware is 32bit or 64bit. This is because generally, the single and double precision formats from the IEEE 754 standard are used for the float and double types respectively (because those formats are conform with the C standard and are in fact recommended by the C standard).
0
ssnkumarCommented:
You are trying to print the max values of some of the float types.
These are fixed values and they will not change across systems.
0
Infinity08Commented:
>> These are fixed values and they will not change across systems.

Yes they can. Which is precisely the reason that FLT_MAX, FLT_MIN etc. values are available.
0
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

Darrell ThomasEngineerAuthor Commented:
So how can I get better floating point precision with the x64?  Or can I?
0
Infinity08Commented:
Use double or long double :)

Or if you really need more, you can always find or create a custom floating point type with the desired precision.

Or if what you're really looking for, is a more consistent handling of precision, then maybe you don't need a floating point type, but rather need a fixed point type.

It all depends on what you need it for.
0
Darrell ThomasEngineerAuthor Commented:
Infinity, that's kind of what I was looking for.  

But maybe I should be more specific:  Is there a floating point precision "advantage" for compiling in 64 bit versus 32 bit?  I suspect not, but I wanted to explore the idea.

Also,  I'd be interested to learn more about creating a custom floating point type.  Don't want to trouble you too much here, but if you have a reference where I could read up on it, (favorite book, fav web tutorial, etc...) that would help.

dt
0
Infinity08Commented:
>> But maybe I should be more specific:  Is there a floating point precision "advantage" for compiling in 64 bit versus 32 bit?

Generally not, no (see my first reply).


>> Also,  I'd be interested to learn more about creating a custom floating point type.

Let me start by telling you that that is not a small task to do (if you want to do it correctly).
Secondly, it will not be a first-order type, so there will be (quite a bit) of overhead - depending on how you implement it.

I wonder though : if a long double doesn't offer you the precision you need ... what are you using this for ? It's very likely that a floating point is not the best tool for what you're doing.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Infinity08Commented:
>> what are you using this for ?

Or in other words : what do you need so much precision for ?
0
Darrell ThomasEngineerAuthor Commented:
I'm not working on anything specific--some goofy problems to explore some numerical method limits.--Non linear dynamic systems with slightly different initial conditions.  Or even the same conditions..  Trying to eliminate as much rounding/truncation error as possible-- as these propagate through the system at a wicked pace.

0
Infinity08Commented:
>> as these propagate through the system at a wicked pace.

Yes. That's one of the biggest downsides of a floating point type. When using floating point types, you need to constantly be aware of this, and compensate for it (by re-ordering the calculations, using a different algorithm, etc.).

It's sometimes a good idea to also consider fixed point types. They often lack the range that floating point types have, but make up for that in precision. Obviously, they're not a panacea, and when used incorrectly they can exhibit the same problems as floating point types.

But all depends on your specific use case.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C

From novice to tech pro — start learning today.