Login
User Name:

Password:



Register
Forgot your password?
Vote for Us!
auth_update crash
Dec 23, 2017, 10:15 pm
By Remcon
check_tumble
Dec 18, 2017, 7:21 pm
By Remcon
parse description bug
Dec 15, 2017, 10:08 pm
By Remcon
Couple bugs
Dec 12, 2017, 5:42 pm
By Remcon
Bug in disarm( )
Nov 12, 2017, 6:54 pm
By GatewaySysop
LoP 1.46
Author: Remcon
Submitted by: Remcon
LOP 1.45
Author: Remcon
Submitted by: Remcon
LOP Heroes Edition
Author: Vladaar
Submitted by: Vladaar
Heroes sound extras
Author: Vladaar
Submitted by: Vladaar
6Dragons 4.3
Author: Vladaar
Submitted by: Vladaar
Users Online
CommonCrawl

Members: 0
Guests: 13
Stats
Files
Topics
Posts
Members
Newest Member
478
3,708
19,242
612
Jacki72H
Today's Birthdays
Evoru (32)
Related Links
» SmaugMuds.org » Codebases » AFKMud Support & Development » fread_float
Forum Rules | Mark all | Recent Posts

fread_float
< Newer Topic :: Older Topic >

Pages:<< prev 1 next >>
Post is unread #1 Mar 13, 2004, 10:52 am
Go to the top of the page
Go to the bottom of the page

Greven
Magician
GroupMembers
Posts204
JoinedMar 5, 2005

AFKMUD has a fread_float function available, but looking at it its exactly the same as fread_number, except for the type it returns. Does AFKMUD use something else to read in the actual fractional values (.00001)?
       
Post is unread #2 Mar 13, 2004, 12:03 pm
Go to the top of the page
Go to the bottom of the page

Samson
Black Hand
GroupAdministrators
Posts3,639
JoinedJan 1, 2002

There doesn't apear to be a problem with fread_float. Test value stuffed into a numattacks filed in an area file was loaded properly.

This line:

-1000 0 0 0 0 150 100 9.87654321

Gave this result upon statting the mob after bootup:

#Attks: 9.876543

So it would appear it's handling the data properly. Was there some issue you were having with getting it to work?
       
Post is unread #3 Mar 13, 2004, 1:16 pm
Go to the top of the page
Go to the bottom of the page

Greven
Magician
GroupMembers
Posts204
JoinedMar 5, 2005

Yes, it seems thats its not reading in anything after and including the decimal. I looked at where it reads in numattacks in AFKMUD, and it seems that it uses sscanf, not fread_float.
       
Post is unread #4 Mar 13, 2004, 1:31 pm
Go to the top of the page
Go to the bottom of the page

Samson
Black Hand
GroupAdministrators
Posts3,639
JoinedJan 1, 2002

Oh. Hey, you know, you're right. Apparenlty fread_float is not actually called anywhere despite it being included. How silly. And yes, I apparenly used sscanf for numattacks. Perhaps accounting for why it worked

Anyway, I was scouring my collection of Merc codebases and was not able to find an fread_* function in any of them that can handle floats, so it's entirely possible this can't be done this way. Either that or nobody has bothered.

If you have any ideas how fread_float could be made to work as intended, I'd love to know. Otherwise I'll probably end up removing it
       
Post is unread #5 Mar 13, 2004, 1:55 pm   Last edited Nov 23, 2007, 11:43 pm by Samson
Go to the top of the page
Go to the bottom of the page

Greven
Magician
GroupMembers
Posts204
JoinedMar 5, 2005

Yeah, I had an idea on how, but at the moment I am unable to test how it would work. The idea I had was allow it to accept . as well. When this happens, set a bool to TRUE signifying we're after the decimal and continue on to the next chararcter. fread_number uses this:
number = number * 10 + c - '0';
I was thinking something along the lines of
if ( !decimal )
    number = number * 10 + c - '0';
else
{
    number += (0.1 * pow(10,place)) * c - '0'
    place++
}
Now, I'm really not that good with math, but I think something like that should work.
       
Post is unread #6 Mar 13, 2004, 2:51 pm
Go to the top of the page
Go to the bottom of the page

Greven
Magician
GroupMembers
Posts204
JoinedMar 5, 2005

well, after tinkering with this stupid thing for a while, I've decided that its easier to write floats with ~, and use atof(fread_string_noalloc(fp)) rather than fread_float.
       
Post is unread #7 Mar 13, 2004, 3:32 pm   Last edited Nov 23, 2007, 11:43 pm by Samson
Go to the top of the page
Go to the bottom of the page

Samson
Black Hand
GroupAdministrators
Posts3,639
JoinedJan 1, 2002

Chaching - you were on the right track, it just needed a small nudge. Here are the results of about an hour's worth of picking at it until it was able to read the Thac0gain values properly from the class files:

float fread_float( FILE *fp )
{
   float number;
   bool sign, decimal;
   char c;
   int place = 0;

   do
   {
      if( feof(fp) )
      {
         bug( "%s", "fread_float: EOF encountered on read." );
         if( fBootDb )
         {
            shutdown_mud( "Corrupt file somewhere." );
            exit(1);
         }
         return 0;
      }
      c = getc( fp );
   }
   while( isspace(c) );

   number = 0;

   sign    = FALSE;
   decimal = FALSE;

   if( c == '+' )
      c = getc( fp );
   else if( c == '-' )
   {
      sign = TRUE;
      c = getc( fp );
   }

   if( !isdigit(c) )
   {
      bug( "Fread_float: bad format. (%c)", c );
      if( fBootDb )
         exit( 1 );
      return 0;
   }

   while( 1 )
   {
      if( c == '.' || isdigit(c) )
      {
         if( c == '.' )
         {
            decimal = TRUE;
            c = getc( fp );
         }

         if( feof(fp) )
         {
            bug( "%s", "fread_float: EOF encountered on read." );
            if( fBootDb )
               exit( 1 );
            return number;
         }
         if( !decimal )
            number = number * 10 + c - '0';
         else
         {
            place++;
            number += pow( 10, ( -1 * place ) ) * ( c - '0' );
         }
         c = getc( fp );
      }
      else break;
   }

   if( sign )
      number = 0 - number;

   if( c == '|' )
      number += fread_float( fp );
   else if( c != ' ' )
      ungetc( c, fp );

   return number;
}


Feel free to verify this against anything you have which also uses float values, but I see no reason it should fail now.
       
Post is unread #8 Mar 13, 2004, 3:35 pm
Go to the top of the page
Go to the bottom of the page

Samson
Black Hand
GroupAdministrators
Posts3,639
JoinedJan 1, 2002

Uh, of course, your method is proably much cleaner, but since I spent the time I'm going to incorporate the version you prodded me into fixing
       
Post is unread #9 Mar 13, 2004, 4:09 pm   Last edited Nov 23, 2007, 11:43 pm by Samson
Go to the top of the page
Go to the bottom of the page

Greven
Magician
GroupMembers
Posts204
JoinedMar 5, 2005

I'm not thinking real clear, so Gavin ended up making this, works nicely as well
/*
 * Read a number from a file.
 */
float fread_float( FILE *fp )
{
    float number;
    char buf[32];
    bool sign;
    char c;
    int count = 0;

    do
    {
     if ( feof(fp) )
     {
      bug("fread_number: EOF encountered on read.";);
      if ( fBootDb )
       exit(1);
      return 0.0f;
     }
     c = getc( fp );
    }
    while ( isspace(c) );

    number = 0;

    sign   = FALSE;
    if ( c == '+' )
     c = getc( fp );
    else if ( c == '-' )
    {
     sign = TRUE;
     c = getc( fp );
    }
    
    if ( !isdigit(c) )
    {
     bug( "Fread_number: bad format. (%c)", c );
     if ( fBootDb )
      abort();
     return 0.0f;
    }
   
    if (isdigit(c))
    {
     buf[count] = c;
     count++;
    }
	
    while ( isdigit(c) || c == '.' )
    {
     if (count >= 31) break;
     if ( feof(fp) )
     {
      bug("fread_number: EOF encountered on read.";);
      if ( fBootDb )
       exit(1);
      return number;
     }
     c      = getc( fp );
     if (c != '0')
      buf[count] = c;
     count++;
     
    }
    buf[count] = '�';
    number = atof(buf);

    if ( sign )
  number = 0 - number;

    if ( c != ' ' )
     ungetc( c, fp );

    return number;
}
       
Post is unread #10 Mar 14, 2004, 10:26 am   Last edited Nov 23, 2007, 11:44 pm by Samson
Go to the top of the page
Go to the bottom of the page

gavin

GroupMembers
Posts2
JoinedDec 16, 2003

Actually, I see a few small bugs in that code anyways.

 if (c != '0')
     buf[count] = c;
    count++;


 if  (c != ' ')
     buf[count] = c;
    count++;
       
Pages:<< prev 1 next >>