OpenCores
URL https://opencores.org/ocsvn/or1k_old/or1k_old/trunk

Subversion Repositories or1k_old

[/] [or1k_old/] [trunk/] [uclinux/] [uC-libc/] [misc/] [strtod.c] - Blame information for rev 1782

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 199 simons
/*
2
 * strtod.c - This file is part of the libc-8086 package for ELKS,
3
 * Copyright (C) 1995, 1996 Nat Friedman <ndf@linux.mit.edu>.
4
 *
5
 *  This library is free software; you can redistribute it and/or
6
 *  modify it under the terms of the GNU Library General Public
7
 *  License as published by the Free Software Foundation; either
8
 *  version 2 of the License, or (at your option) any later version.
9
 *
10
 *  This library is distributed in the hope that it will be useful,
11
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13
 *  Library General Public License for more details.
14
 *
15
 *  You should have received a copy of the GNU Library General Public
16
 *  License along with this library; if not, write to the Free
17
 *  Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18
 *
19
 */
20
#include <stdlib.h>
21
#include <ctype.h>
22
 
23
float
24
strtod(const char *nptr, char ** endptr)
25
{
26
  unsigned short negative;
27
  float number;
28
  float fp_part;
29
  int exponent;
30
  unsigned short exp_negative;
31
 
32
  /* advance beyond any leading whitespace */
33
  while (isspace(*nptr))
34
    nptr++;
35
 
36
  /* check for optional '+' or '-' */
37
  negative=0;
38
  if (*nptr=='-')
39
    {
40
      negative=1;
41
      nptr++;
42
    }
43
  else
44
    if (*nptr=='+')
45
      nptr++;
46
 
47
  number=0;
48
  while (isdigit(*nptr))
49
    {
50
      number=number*10+(*nptr-'0');
51
      nptr++;
52
    }
53
 
54
  if (*nptr=='.')
55
    {
56
      nptr++;
57
      fp_part=0;
58
      while (isdigit(*nptr))
59
        {
60
          fp_part=fp_part/10.0 + (*nptr-'0')/10.0;
61
          nptr++;
62
        }
63
      number+=fp_part;
64
    }
65
 
66
  if (*nptr=='e' || *nptr=='E')
67
    {
68
      nptr++;
69
      exp_negative=0;
70
      if (*nptr=='-')
71
        {
72
          exp_negative=1;
73
          nptr++;
74
        }
75
      else
76
        if (*nptr=='+')
77
          nptr++;
78
 
79
      exponent=0;
80
      while (isdigit(*nptr))
81
        {
82
          exponent=exponent*10+(*nptr-'0');
83
          exponent++;
84
        }
85
    }
86
 
87
  while (exponent)
88
    {
89
      if (exp_negative)
90
        number/=10;
91
      else
92
        number*=10;
93
      exponent--;
94
    }
95
  return (negative ? -number:number);
96
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.