The mathematical definition of the cosh function is:
Since exponential operations are involved, when the input parameters are large, the calculation results of cosh will grow rapidly, resulting in very large values. These large values may cause axial scale imbalance, graph distortion, and even crashes in the drawing function when drawing.
In addition, the value range of cosh is , there are no negative values, so when designing charts, you should also avoid errors caused when misusing negative values or logarithmic transformations.
Data overflow or infinity <br> For example, when the incoming parameters are very large, the result of the cosh function will exceed the range of PHP floating point numbers, resulting in INF or NaN , which will cause the chart library to be unable to process.
Unreasonable coordinate axis ratio <br> Due to the rapid growth of cosh , small-scale inputs cause huge changes in output, making it difficult to present details when drawing.
Misuse of logarithmic coordinates <br> If the cosh result is used directly on the logarithmic axis, an error will be raised when a 0 or negative number appears.
Reasonably limit the input value to avoid passing in parameters that are too large or too small. For example:
function safe_cosh($x) {
// Limit the input range,Avoid overflow
if ($x > 20) {
$x = 20;
} elseif ($x < -20) {
$x = -20;
}
return cosh($x);
}
Detect the cosh result and replace the infinity or illegal value:
function safe_cosh_value($x) {
$value = cosh($x);
if (!is_finite($value)) {
return PHP_FLOAT_MAX; // or other suitable maximum value replacement
}
return $value;
}
Normalize or logarithmic transformation of data to alleviate numerical differences:
function normalize_data(array $data) {
$max = max($data);
$min = min($data);
if ($max == $min) {
return $data;
}
return array_map(function($v) use ($max, $min) {
return ($v - $min) / ($max - $min);
}, $data);
}
Use drawing libraries that support large number processing
Set a reasonable range of coordinate axes
Avoid logarithm operations on the results directly, or filter 0 and negative values first
The following sample code demonstrates how to safely calculate cosh and output JSON format data to facilitate the drawing of front-end drawing library.
<?php
function safe_cosh($x) {
if ($x > 20) {
$x = 20;
} elseif ($x < -20) {
$x = -20;
}
$value = cosh($x);
if (!is_finite($value)) {
return PHP_FLOAT_MAX;
}
return $value;
}
$input_range = range(-10, 10, 0.5);
$data = [];
foreach ($input_range as $x) {
$data[] = [
'x' => $x,
'y' => safe_cosh($x),
];
}
// NormalizationYvalue,Convenient chart display
$y_values = array_column($data, 'y');
$y_norm = normalize_data($y_values);
foreach ($data as $key => $point) {
$data[$key]['y_normalized'] = $y_norm[$key];
}
header('Content-Type: application/json');
echo json_encode($data);
function normalize_data(array $data) {
$max = max($data);
$min = min($data);
if ($max == $min) {
return $data;
}
return array_map(function($v) use ($max, $min) {
return ($v - $min) / ($max - $min);
}, $data);
}
URL to access the sample data:
https://gitbox.net/path/to/your/api/cosh-data.php
The front-end can request the interface to obtain secure cosh data through AJAX and then draw a chart of reasonable proportions.
When calculating using PHP's cosh function, be sure to pay attention to the processing of input range and result values to prevent numerical overflow and chart proportional imbalance. By limiting input, detecting outliers, normalizing data and rationally configuring chart parameters, chart drawing errors can be effectively avoided and the accuracy and aesthetics of data visualization can be ensured.