<commit_before>#include "number.h"
#include "value.h"

/* ignore unused parameter warnings */
#pragma GCC diagnostic ignored "-Wunused-parameter"

/***** mathmatical functions ******/

/* number?
 */
struct plot_value * plot_func_number_test(struct plot_env *env, struct plot_value *args){
    plot_value *val;

    if( args->type != plot_type_pair || cdr(args)->type != plot_type_null ){
        return plot_runtime_error(plot_error_bad_args, "expected exactly 1 arg", "plot_func_number_test");
    }

    val = car(args);

    if( ! val ){
        return plot_runtime_error(plot_error_internal, "arg was null", "plot_func_number_test");
    }

    return plot_new_boolean( val->type == plot_type_number );
}

/* (exact? obj)
 */
struct plot_value * plot_func_exact_test(struct plot_env *env, struct plot_value *args){
    plot_value *val;

    if( args->type != plot_type_pair || cdr(args)->type != plot_type_null ){
        return plot_runtime_error(plot_error_bad_args, "expected exactly 1 arg", "plot_func_exact_test");
    }

    val = car(args);

    if( ! val ){
        return plot_runtime_error(plot_error_internal, "arg was null", "plot_func_exact_test");
    }

    return plot_new_boolean( val->type == plot_type_number );
}

/* (+ number1 number2 ...)
 * takes a list of expressions
 * evals each and then adds the expression's value if it is a number
 * if any of the expressions evaluate to something other than an error
 * throw plot_error_bad_args
 */
plot_value * plot_func_add(struct plot_env *env, plot_value *args){
    plot_value *arg;
    int sum=0;

    for( arg = args; arg->type == plot_type_pair; arg = cdr(arg) ){
        if( car(arg)->type != plot_type_number ){
            return plot_runtime_error(plot_error_bad_args, "arg was not of type plot_type_number", "plot_func_add");
        }

        sum += car(arg)->u.number.val;
    }

    return plot_new_number(sum);
}

/* (- number1 number2 ...)
 * takes a list of expressions
 * evals each and then subtracts the expression's value if it is a number
 * if any of the expressions evaluate to something other than an error
 * throw plot_error_bad_args
 */
plot_value * plot_func_subtract(struct plot_env *env, plot_value *args){
    plot_value *arg;
    int difference = 0;

    if( car(args)->type != plot_type_number ){
        return plot_runtime_error(plot_error_bad_args, "argument is not a number", "plot_func_subtract");
    }

    difference = car(args)->u.number.val;

    for( arg = cdr(args); arg->type == plot_type_pair; arg = cdr(arg) ){
        if( car(arg)->type != plot_type_number ){
            return plot_runtime_error(plot_error_bad_args, "argument is not a number", "plot_func_subtract");
        }

        difference -= car(arg)->u.number.val;
    }

    return plot_new_number(difference);
}

/* (* number1 number2 ...)
 * takes a list of expressions
 * evals each and then multiplies the expression's value if it is a number
 * if any of the expressions evaluate to something other than an error
 * throw plot_error_bad_args
 */
plot_value * plot_func_multiply(struct plot_env *env, plot_value *args){
    plot_value *arg;
    int product=1;

    for( arg = args; arg->type == plot_type_pair; arg = cdr(arg) ){
        if( car(arg)->type != plot_type_number ){
            return plot_runtime_error(plot_error_bad_args, "argument is not a number", "plot_func_subtract");
        }

        product *= car(arg)->u.number.val;
    }

    return plot_new_number(product);
}

/* (/ number1 number2 ...)
 * integer division
 * exact only
 */
struct plot_value * plot_func_divide(struct plot_env *env, struct plot_value *args){
    plot_value *arg;
    int quotient=0;

    if( car(args)->type != plot_type_number ){
        return plot_runtime_error(plot_error_bad_args, "argument is not a number", "plot_func_divide");
    }

    quotient = car(args)->u.number.val;

    for( arg = args; arg->type == plot_type_pair; arg = cdr(arg) ){<commit_msg>fixing bug in divide implementation<commit_after>#include "number.h"
#include "value.h"

/* ignore unused parameter warnings */
#pragma GCC diagnostic ignored "-Wunused-parameter"

/***** mathmatical functions ******/

/* number?
 */
struct plot_value * plot_func_number_test(struct plot_env *env, struct plot_value *args){
    plot_value *val;

    if( args->type != plot_type_pair || cdr(args)->type != plot_type_null ){
        return plot_runtime_error(plot_error_bad_args, "expected exactly 1 arg", "plot_func_number_test");
    }

    val = car(args);

    if( ! val ){
        return plot_runtime_error(plot_error_internal, "arg was null", "plot_func_number_test");
    }

    return plot_new_boolean( val->type == plot_type_number );
}

/* (exact? obj)
 */
struct plot_value * plot_func_exact_test(struct plot_env *env, struct plot_value *args){
    plot_value *val;

    if( args->type != plot_type_pair || cdr(args)->type != plot_type_null ){
        return plot_runtime_error(plot_error_bad_args, "expected exactly 1 arg", "plot_func_exact_test");
    }

    val = car(args);

    if( ! val ){
        return plot_runtime_error(plot_error_internal, "arg was null", "plot_func_exact_test");
    }

    return plot_new_boolean( val->type == plot_type_number );
}

/* (+ number1 number2 ...)
 * takes a list of expressions
 * evals each and then adds the expression's value if it is a number
 * if any of the expressions evaluate to something other than an error
 * throw plot_error_bad_args
 */
plot_value * plot_func_add(struct plot_env *env, plot_value *args){
    plot_value *arg;
    int sum=0;

    for( arg = args; arg->type == plot_type_pair; arg = cdr(arg) ){
        if( car(arg)->type != plot_type_number ){
            return plot_runtime_error(plot_error_bad_args, "arg was not of type plot_type_number", "plot_func_add");
        }

        sum += car(arg)->u.number.val;
    }

    return plot_new_number(sum);
}

/* (- number1 number2 ...)
 * takes a list of expressions
 * evals each and then subtracts the expression's value if it is a number
 * if any of the expressions evaluate to something other than an error
 * throw plot_error_bad_args
 */
plot_value * plot_func_subtract(struct plot_env *env, plot_value *args){
    plot_value *arg;
    int difference = 0;

    if( car(args)->type != plot_type_number ){
        return plot_runtime_error(plot_error_bad_args, "argument is not a number", "plot_func_subtract");
    }

    difference = car(args)->u.number.val;

    for( arg = cdr(args); arg->type == plot_type_pair; arg = cdr(arg) ){
        if( car(arg)->type != plot_type_number ){
            return plot_runtime_error(plot_error_bad_args, "argument is not a number", "plot_func_subtract");
        }

        difference -= car(arg)->u.number.val;
    }

    return plot_new_number(difference);
}

/* (* number1 number2 ...)
 * takes a list of expressions
 * evals each and then multiplies the expression's value if it is a number
 * if any of the expressions evaluate to something other than an error
 * throw plot_error_bad_args
 */
plot_value * plot_func_multiply(struct plot_env *env, plot_value *args){
    plot_value *arg;
    int product=1;

    for( arg = args; arg->type == plot_type_pair; arg = cdr(arg) ){
        if( car(arg)->type != plot_type_number ){
            return plot_runtime_error(plot_error_bad_args, "argument is not a number", "plot_func_subtract");
        }

        product *= car(arg)->u.number.val;
    }

    return plot_new_number(product);
}

/* (/ number1 number2 ...)
 * integer division
 * exact only
 */
struct plot_value * plot_func_divide(struct plot_env *env, struct plot_value *args){
    plot_value *arg;
    int quotient=0;

    if( car(args)->type != plot_type_number ){
        return plot_runtime_error(plot_error_bad_args, "argument is not a number", "plot_func_divide");
    }

    quotient = car(args)->u.number.val;

    for( arg = cdr(args); arg->type == plot_type_pair; arg = cdr(arg) ){
{'fraction_non_alphanumeric': 0.11956110480514567, 'fraction_numerical': 0.0035313406482532475, 'mean_word_length': 3.5236737022247575, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 43, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5343091', 'n_tokens_mistral': 2585, 'n_tokens_neox': 2504, 'n_words': 686}