Numerus  v2.0.0
Roman numerals conversion and manipulation C library.
numerus_core.c File Reference

Numerus constants and functions for roman numerals conversion. More...

#include <ctype.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
#include "numerus_internal.h"

Go to the source code of this file.

Functions

double numerus_roman_to_double (char *roman, int *errcode)
 Converts a roman numeral to its value expressed as a double. More...
 
long numerus_roman_to_int (char *roman, int *errcode)
 Converts a roman numeral to its value floored and expressed as long integer. More...
 
long numerus_roman_to_int_part_and_twelfths (char *roman, short *twelfths, int *errcode)
 Converts a roman numeral to its value expressed as pair of its integer part and number of twelfths. More...
 
char * numerus_int_to_roman (long int_value, int *errcode)
 Converts a long integer value to a roman numeral with its value. More...
 
char * numerus_double_to_roman (double double_value, int *errcode)
 Converts a double value to a roman numeral with its value. More...
 
char * numerus_int_with_twelfth_to_roman (long int_part, short twelfths, int *errcode)
 Converts an integer value and a number of twelfths to a roman numeral with their sum as value. More...
 

Variables

const long NUMERUS_MAX_LONG_NONFLOAT_VALUE = 3999999
 The maximum value a roman numeral with underscores without decimals may have.
 
const double NUMERUS_MAX_VALUE = NUMERUS_MAX_LONG_NONFLOAT_VALUE + 11.5 / 12.0
 The maximum value a roman numeral may have. More...
 
const long int NUMERUS_MIN_LONG_NONFLOAT_VALUE = -NUMERUS_MAX_LONG_NONFLOAT_VALUE
 The minimum value a roman numeral with underscores without decimals may have. More...
 
const double NUMERUS_MIN_VALUE = -NUMERUS_MAX_VALUE
 The minimum value a roman numeral may have. More...
 
const double NUMERUS_MAX_NONLONG_FLOAT_VALUE = 3999 + 11.5 / 12.0
 The maximum value a roman numeral without underscores with decimals may have. More...
 
const double NUMERUS_MIN_NONLONG_FLOAT_VALUE = -NUMERUS_MAX_NONLONG_FLOAT_VALUE
 The minimum value a roman numeral without underscores with decimals may have. More...
 
const char * NUMERUS_ZERO = "NULLA"
 The roman numeral of value 0 (zero). More...
 
const short int NUMERUS_MAX_LENGTH = 37
 The maximum length a roman numeral string may have, including '\0'. More...
 
int numerus_error_code = NUMERUS_OK
 The global error code variable to store any errors during conversions. More...
 

Detailed Description

Numerus constants and functions for roman numerals conversion.

License:
This file is part of the Numerus project which is released under the BSD 3-clause license.

This file contains all constants with special values or extremes as well as the conversion functions from value to roman numeral and vice versa. Some functions from the numerus_utils.c file are also used to perform some checks on the numerals or to convert some values in other formats.

Definition in file numerus_core.c.

Function Documentation

char* numerus_double_to_roman ( double  double_value,
int *  errcode 
)

Converts a double value to a roman numeral with its value.

Accepts any long within [NUMERUS_MAX_VALUE, NUMERUS_MIN_VALUE]. The decimal part of the value is also converted.

Remember to free() the roman numeral when it's not useful anymore.

The conversion status is stored in the errcode passed as parameter, which can be NULL to ignore the error, although it's not recommended. If the the error code is different than NUMERUS_OK, an error occurred during the conversion and the returned string is NULL. The error code may help find the specific error.

Parameters
double_valuedouble precision floating point value to be converted to roman numeral.
*errcodeint where to store the conversion status: NUMERUS_OK or any other error. Can be NULL to ignore the error (NOT recommended).
Returns
char* a string containing the roman numeral or NULL when an error occurs.

Definition at line 726 of file numerus_core.c.

char* numerus_int_to_roman ( long  int_value,
int *  errcode 
)

Converts a long integer value to a roman numeral with its value.

Accepts any long within [NUMERUS_MAX_LONG_NONFLOAT_VALUE, NUMERUS_MIN_LONG_NONFLOAT_VALUE].

Remember to free() the roman numeral when it's not useful anymore.

The conversion status is stored in the errcode passed as parameter, which can be NULL to ignore the error, although it's not recommended. If the the error code is different than NUMERUS_OK, an error occurred during the conversion and the returned string is NULL. The error code may help find the specific error.

Parameters
int_valuelong integer to be converted to roman numeral.
*errcodeint where to store the conversion status: NUMERUS_OK or any other error. Can be NULL to ignore the error (NOT recommended).
Returns
char* a string containing the roman numeral or NULL when an error occurs.

Definition at line 700 of file numerus_core.c.

char* numerus_int_with_twelfth_to_roman ( long  int_part,
short  twelfths,
int *  errcode 
)

Converts an integer value and a number of twelfths to a roman numeral with their sum as value.

Accepts any pair of integer value and twelfths so that their sum is within [NUMERUS_MAX_VALUE, NUMERUS_MIN_VALUE].

Remember to free() the roman numeral when it's not useful anymore.

The conversion status is stored in the errcode passed as parameter, which can be NULL to ignore the error, although it's not recommended. If the the error code is different than NUMERUS_OK, an error occurred during the conversion and the returned string is NULL. The error code may help find the specific error.

Parameters
int_partlong integer part of a value to be added to the twelfths and converted to roman numeral.
twelfthsshort integer as number of twelfths (1/12) to be added to the integer part and converted to roman numeral.
*errcodeint where to store the conversion status: NUMERUS_OK or any other error. Can be NULL to ignore the error (NOT recommended).
Returns
char* a string containing the roman numeral or NULL when an error occurs.

Definition at line 757 of file numerus_core.c.

double numerus_roman_to_double ( char *  roman,
int *  errcode 
)

Converts a roman numeral to its value expressed as a double.

Accepts many variations of roman numerals:

  • it's case INsensitive
  • accepts negative roman numerals (with leading minus '-')
  • accepts long roman numerals (with character between underscores to denote the part that has a value multiplied by 1000)
  • accepts decimal value of the roman numerals, those are twelfths (with the characters 'S' and dot '.')
  • all combinations of the above

The parsing status of the roman numeral (any kind of wrong syntax) is stored in the errcode passed as parameter, which can be NULL to ignore the error, although it's not recommended. If the the error code is different than NUMERUS_OK, an error occurred during the conversion and the returned value is outside the possible range of values of roman numerals. The error code may help find the specific error.

Parameters
*romanstring with a roman numeral
*errcodeint where to store the conversion status: NUMERUS_OK or any other error. Can be NULL to ignore the error (NOT recommended).
Returns
double value of the roman numeral or a value outside the the possible range of values when an error occurs.

Definition at line 474 of file numerus_core.c.

long numerus_roman_to_int ( char *  roman,
int *  errcode 
)

Converts a roman numeral to its value floored and expressed as long integer.

Accepts many variations of roman numerals:

  • it's case INsensitive
  • accepts negative roman numerals (with leading minus '-')
  • accepts long roman numerals (with character between underscores to denote the part that has a value multiplied by 1000)
  • accepts decimal value of the roman numerals, those are twelfths (with the characters 'S' and dot '.')
  • all combinations of the above

The parsing status of the roman numeral (any kind of wrong syntax) is stored in the errcode passed as parameter, which can be NULL to ignore the error, although it's not recommended. If the the error code is different than NUMERUS_OK, an error occurred during the conversion and the returned value is outside the possible range of values of roman numerals. The error code may help find the specific error.

Parameters
*romanstring with a roman numeral
*errcodeint where to store the conversion status: NUMERUS_OK or any other error. Can be NULL to ignore the error (NOT recommended).
Returns
long value of the roman numeral or a value outside the the possible range of values when an error occurs.

Definition at line 508 of file numerus_core.c.

long numerus_roman_to_int_part_and_twelfths ( char *  roman,
short *  twelfths,
int *  errcode 
)

Converts a roman numeral to its value expressed as pair of its integer part and number of twelfths.

Accepts many variations of roman numerals:

  • it's case INsensitive
  • accepts negative roman numerals (with leading minus '-')
  • accepts long roman numerals (with character between underscores to denote the part that has a value multiplied by 1000)
  • accepts decimal value of the roman numerals, those are twelfths (with the characters 'S' and dot '.')
  • all combinations of the above

The parsing status of the roman numeral (any kind of wrong syntax) is stored in the errcode passed as parameter, which can be NULL to ignore the error, although it's not recommended. If the the error code is different than NUMERUS_OK, an error occurred during the conversion and the returned value is outside the possible range of values of roman numerals. The error code may help find the specific error.

The number of twelfths is stored in the passed parameter, while the integer part is returned directly.

Parameters
*romanstring with a roman numeral
*errcodeint where to store the conversion status: NUMERUS_OK or any other error. Can be NULL to ignore the error (NOT recommended).
*twelfthsnumber of twelfths from 0 to 11. NULL is interpreted as 0 twelfths.
Returns
long as the integer part of the value of the roman numeral or a value outside the the possible range of values when an error occurs.

Definition at line 545 of file numerus_core.c.

Variable Documentation

int numerus_error_code = NUMERUS_OK

The global error code variable to store any errors during conversions.

It may contain any of the NUMERUS_ERROR_* error codes or NUMERUS_OK.

Definition at line 105 of file numerus_core.c.

const short int NUMERUS_MAX_LENGTH = 37

The maximum length a roman numeral string may have, including '\0'.

The roman numeral "-_MMMDCCCLXXXVIII_DCCCLXXXVIIIS....." (value: -3888888, -11/12) + \0 is a string long 36+1 = 37 chars.

Definition at line 93 of file numerus_core.c.

const double NUMERUS_MAX_NONLONG_FLOAT_VALUE = 3999 + 11.5 / 12.0

The maximum value a roman numeral without underscores with decimals may have.

If you floor() this value, you get the maximum value the standard roman syntax allows.

Definition at line 67 of file numerus_core.c.

const double NUMERUS_MAX_VALUE = NUMERUS_MAX_LONG_NONFLOAT_VALUE + 11.5 / 12.0

The maximum value a roman numeral may have.

This is for a positive long and float roman numeral. It exceeds the actual maximum value by 1/24 because conversion functions round the value to the nearest 12th.

Definition at line 40 of file numerus_core.c.

const long int NUMERUS_MIN_LONG_NONFLOAT_VALUE = -NUMERUS_MAX_LONG_NONFLOAT_VALUE

The minimum value a roman numeral with underscores without decimals may have.

It's the opposite of NUMERUS_MAX_LONG_NONFLOAT_VALUE.s

Definition at line 48 of file numerus_core.c.

const double NUMERUS_MIN_NONLONG_FLOAT_VALUE = -NUMERUS_MAX_NONLONG_FLOAT_VALUE

The minimum value a roman numeral without underscores with decimals may have.

If you ceil() this value, you get the minimum value the standard roman syntax allows.

Definition at line 76 of file numerus_core.c.

const double NUMERUS_MIN_VALUE = -NUMERUS_MAX_VALUE

The minimum value a roman numeral may have.

It's the opposite of NUMERUS_MAX_VALUE. This is for a negative long and float roman numeral. It is below the actual maximum value by 1/24 because conversion functions round the value to the nearest 12th.

Definition at line 58 of file numerus_core.c.

const char* NUMERUS_ZERO = "NULLA"

The roman numeral of value 0 (zero).

Both for positive and negative zero.

Definition at line 84 of file numerus_core.c.