Changeset 992ffa6 in mainline for uspace/app/tester/float/float2.c


Ignore:
Timestamp:
2015-09-04T06:40:20Z (9 years ago)
Author:
Jiri Svoboda <jiri@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
01cdd5a
Parents:
bae1e1f
Message:

Add exp(f), log(f), pow(f). Improve precision of sin, cos.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • uspace/app/tester/float/float2.c

    rbae1e1f r992ffa6  
    11/*
    22 * Copyright (c) 2014 Martin Decky
     3 * Copyright (c) 2015 Jiri Svoboda
    34 * All rights reserved.
    45 *
     
    2728 */
    2829
     30#include <stdbool.h>
    2931#include <stdio.h>
    3032#include <stdlib.h>
     
    3234#include "../tester.h"
    3335
    34 #define OPERANDS   10
    35 #define PRECISION  100000000
     36#define OPERANDS         10
     37#define PRECISIONF    10000
     38#define PRECISION 100000000
    3639
    3740static double arguments[OPERANDS] = {
    3841        3.5, -2.1, 100.0, 50.0, -1024.0, 0.0, 768.3156, 1080.499999, -600.0, 1.0
    3942};
     43
     44static double arguments_exp[OPERANDS] = {
     45        3.5, -2.1, 50.0, 0.0, 1.0, 13.2, -1.1, -5.5, 0.1, -66.0
     46};
     47
     48static double arguments_log[OPERANDS] = {
     49        3.5, 100.0, 50.0, 768.3156, 1080.499999, 1.0, 66.0,
     50        2.718281828459045, 9.9, 0.001
     51};
     52
    4053
    4154static double results_ceil[OPERANDS] = {
     
    6376};
    6477
     78static double results_log[OPERANDS] = {
     79        1.252762968495, 4.605170185988, 3.912023005428, 6.644200586236,
     80        6.985179175021, 0.000000000000, 4.189654742026, 1.000000000000,
     81        2.292534757141, -6.907755278982
     82};
     83
     84static double results_exp[OPERANDS] = {
     85        33.115451958692, 0.122456428253, 5184705528587072045056.0,
     86        1.000000000000, 2.718281828459, 540364.937246691552, 0.332871083698,
     87        0.004086771438, 1.105170918076, 0.000000000000
     88};
     89
     90static bool cmp_float(float a, float b)
     91{
     92        float r;
     93
     94        /* XXX Need fabsf() */
     95        if (b < 1.0 / PRECISIONF && b > -1.0 / PRECISIONF)
     96                r = a;
     97        else
     98                r = a / b - 1.0;
     99
     100        /* XXX Need fabsf() */
     101        if (r < 0.0)
     102                r = -r;
     103
     104        return r < 1.0 / PRECISIONF;
     105}
     106
     107static bool cmp_double(double a, double b)
     108{
     109        double r;
     110
     111        /* XXX Need fabs() */
     112        if (b < 1.0 / PRECISION && b > -1.0 / PRECISION)
     113                r = a;
     114        else
     115                r = a / b - 1.0;
     116
     117        /* XXX Need fabs() */
     118        if (r < 0.0)
     119                r = -r;
     120
     121        return r < 1.0 / PRECISION;
     122}
     123
    65124const char *test_float2(void)
    66125{
    67126        bool fail = false;
    68        
     127
    69128        for (unsigned int i = 0; i < OPERANDS; i++) {
    70129                double res = floor(arguments[i]);
     
    73132               
    74133                if (res_int != corr_int) {
    75                         TPRINTF("Double floor failed (%" PRId64 " != %" PRId64
    76                             ", arg %u)\n", res_int, corr_int, i);
     134                        TPRINTF("Double precision floor failed (%" PRId64
     135                            " != %" PRId64 ", arg %u)\n", res_int, corr_int, i);
    77136                        fail = true;
    78137                }
     
    85144               
    86145                if (res_int != corr_int) {
    87                         TPRINTF("Double ceil failed (%" PRId64 " != %" PRId64
    88                             ", arg %u)\n", res_int, corr_int, i);
     146                        TPRINTF("Double precision ceil failed (%" PRId64
     147                            " != %" PRId64 ", arg %u)\n", res_int, corr_int, i);
    89148                        fail = true;
    90149                }
     
    97156               
    98157                if (res_int != corr_int) {
    99                         TPRINTF("Double truncation failed (%" PRId64 " != %" PRId64
    100                             ", arg %u)\n", res_int, corr_int, i);
     158                        TPRINTF("Double precisiontruncation failed (%" PRId64
     159                            " != %" PRId64 ", arg %u)\n", res_int, corr_int, i);
    101160                        fail = true;
    102161                }
     
    109168               
    110169                if (res_int != corr_int) {
    111                         TPRINTF("Double sine failed (%" PRId64 " != %" PRId64
    112                             ", arg %u)\n", res_int, corr_int, i);
     170                        TPRINTF("Double precision sine failed (%" PRId64
     171                            " != %" PRId64 ", arg %u)\n", res_int, corr_int, i);
    113172                        fail = true;
    114173                }
     
    121180               
    122181                if (res_int != corr_int) {
    123                         TPRINTF("Double cosine failed (%" PRId64 " != %" PRId64
    124                             ", arg %u)\n", res_int, corr_int, i);
     182                        TPRINTF("Double precision cosine failed (%" PRId64
     183                            " != %" PRId64 ", arg %u)\n", res_int, corr_int, i);
     184                        fail = true;
     185                }
     186        }
     187       
     188        for (unsigned int i = 0; i < OPERANDS; i++) {
     189                float res = logf(arguments_log[i]);
     190               
     191                if (!cmp_float(res, results_log[i])) {
     192                        TPRINTF("Single precision logarithm failed "
     193                            "(%lf != %lf, arg %u)\n", res, results_log[i], i);
     194                        fail = true;
     195                }
     196        }
     197       
     198        for (unsigned int i = 0; i < OPERANDS; i++) {
     199                double res = log(arguments_log[i]);
     200               
     201                if (!cmp_double(res, results_log[i])) {
     202                        TPRINTF("Double precision logarithm failed "
     203                            "(%lf != %lf, arg %u)\n", res, results_log[i], i);
     204                        fail = true;
     205                }
     206        }
     207       
     208        for (unsigned int i = 0; i < OPERANDS; i++) {
     209                float res = exp(arguments_exp[i]);
     210               
     211                if (!cmp_float(res, results_exp[i])) {
     212                        TPRINTF("Single precision exponential failed "
     213                            "(%lf != %lf, arg %u)\n", res, results_exp[i], i);
     214                        fail = true;
     215                }
     216        }
     217       
     218        for (unsigned int i = 0; i < OPERANDS; i++) {
     219                double res = exp(arguments_exp[i]);
     220               
     221                if (!cmp_double(res, results_exp[i])) {
     222                        TPRINTF("Double precision exponential failed "
     223                            "(%lf != %lf, arg %u)\n", res, results_exp[i], i);
    125224                        fail = true;
    126225                }
Note: See TracChangeset for help on using the changeset viewer.