Subversion Repositories shark

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
2 pj 1
/* Project:     OSLib
2
 * Description: The OS Construction Kit
3
 * Date:                1.6.2000
4
 * Idea by:             Luca Abeni & Gerardo Lamastra
5
 *
6
 * OSLib is an SO project aimed at developing a common, easy-to-use
7
 * low-level infrastructure for developing OS kernels and Embedded
8
 * Applications; it partially derives from the HARTIK project but it
9
 * currently is independently developed.
10
 *
11
 * OSLib is distributed under GPL License, and some of its code has
12
 * been derived from the Linux kernel source; also some important
13
 * ideas come from studying the DJGPP go32 extender.
14
 *
15
 * We acknowledge the Linux Community, Free Software Foundation,
16
 * D.J. Delorie and all the other developers who believe in the
17
 * freedom of software and ideas.
18
 *
19
 * For legalese, check out the included GPL license.
20
 */
21
 
22
 
23
#include <ll/i386/stdlib.h>
24
#include <ll/i386/string.h>
25
#include <ll/i386/limits.h>
26
#include <ll/i386/float.h>
27
#include <ll/i386/mem.h>
28
#include <ll/stdarg.h>
29
#include <ll/ctype.h>
30
#include <ll/math.h>
31
#include "sprintf.h"
32
 
33
FILE(ecvt);
34
 
35
unsigned ecvt(double v,char *buffer,int width,int prec,int flag)
36
{
37
    register int len=0;
38
    long int exponent,abs_exponent;
39
    /* Manage Inf & NaN */
40
    if (isspecial(v,buffer)) return(strlen(buffer));
41
    /* Check for negative value & add optional + sign */
42
    if (v < 0) {
43
        *buffer++ = '-';
44
        v = -v;
45
        len++;
46
    } else if (flag & ADD_PLUS) {
47
        *buffer++ = '+';
48
        len++;
49
    }  
50
    /* Zero check! */
51
    if (v < DBL_MIN) {
52
        *buffer++ = '0';
53
        *buffer = 0;
54
        return(1);
55
    }
56
    /* Evaluate the exponent */
57
    if (v < 1) {
58
        exponent = 0;
59
        while (v < 1.0) {
60
            v *= 10.0;
61
            exponent--;
62
        }
63
    }
64
    else {
65
        exponent = 0;
66
        while (v >= 10.0) {
67
            v /= 10.0;
68
            exponent++;
69
        }
70
    }
71
    abs_exponent = abs(exponent);
72
    if (abs_exponent > 99) width -= 3;
73
    else if (abs_exponent > 9) width -= 2;
74
    else if (exponent > 0) width -= 1;
75
    /* Now the number as IP in range [0,1] */
76
    /* Convert this as a fixed point format */
77
    len += fcvt(v,buffer,width,prec,flag | RESPECT_WIDTH);
78
    /* Now convert the exponent */
79
    if (exponent == 0) return(len);
80
    buffer += len;
81
    *buffer++ = 'e';
82
    len += dcvt(exponent,buffer,10,exponent/10+2,ADD_PLUS)+1;
83
    return(len);
84
}