Thread: Floating point numbers in a binary file

  1. #1

    Floating point numbers in a binary file

    I'm making a 3D model format for my projects, and I'm stuck. I want to use floating point numbers for coordinates, but I can't think of an efficient way to have floating point numbers in a binary file.

    PS: I am using cstdio for loading the file and OpenGL for the graphics

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    Why can't you just write the floating-point numbers to file with fwrite just like everything else?
    My best code is written with the delete key.

  3. #3
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    Personally I would use doubles and it should not be any more of a problem to write floating point values to disk than writing integral values.

  4. #4
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    well I don't know what cstudio is but like prelude and bubba already said, its just as easy to write out floats as any data type.

    As far as doubles go, it may be a game or something where tremendous precision is not required.
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  5. #5
    I'm using an hex editor to make the file (my test files for new formats I always write by hand), and all I can input is hex values. A period isn't really a hex value.

    I was thinking about making everything a word, and the first byte would be left of the decimal, and the second byte would be right of it.

  6. #6
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    um... that's going to be a poor format for your floats. The way its done naturally is with a mantissa, exponent, etc... I'll see if I can't find a reference.

    edit:
    here you go:
    http://odin.prohosting.com/~ujdesk/floats/floats.pdf
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  7. #7


    Okay, I guess I'm just going to stick with fixed point decimals. I'm just going to include in my header of the file how many bytes represent the left of the decimal, and how many represent the right of it. I'll just assume everything is positive, and I can just translate it how I want in the program.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. gcc link external library
    By spank in forum C Programming
    Replies: 6
    Last Post: 08-08-2007, 03:44 PM
  2. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  3. copying binary file
    By samc2004 in forum C Programming
    Replies: 5
    Last Post: 12-09-2003, 01:34 PM
  4. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM