Skip to content

Commit

Permalink
A (hopefully) more universal fix for the signed max negative value pr…
Browse files Browse the repository at this point in the history
…inter
  • Loading branch information
mvandervoord committed Sep 1, 2014
1 parent e2d5e1c commit f480051
Showing 1 changed file with 11 additions and 5 deletions.
16 changes: 11 additions & 5 deletions src/unity.c
Original file line number Diff line number Diff line change
Expand Up @@ -125,18 +125,24 @@ void UnityPrintNumber(const _U_SINT number_to_print)
{
_U_SINT divisor = 1;
_U_SINT next_divisor;

_U_UINT number;

if (number_to_print < 0)
if (number_to_print == (1l << (UNITY_LONG_WIDTH-1)))
{
//The largest representable negative number
UNITY_OUTPUT_CHAR('-');
number = -number_to_print;
number = (_U_UINT)number_to_print;
}
else if (number_to_print < 0)
{
//Some other negative number
UNITY_OUTPUT_CHAR('-');
number = (_U_UINT)(-number_to_print);
}
else
{
number = number_to_print;

//Positive number
number = (_U_UINT)number_to_print;
}

// figure out initial divisor
Expand Down

2 comments on commit f480051

@jwalkerbg
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It could be easier ... if "(number_to_print == (1l << (UNITY_LONG_WIDTH-1)))" is true at L130, then the line 134 could use the constant value "(1l << (UNITY_LONG_WIDTH-1))". There is no need to use "number_to_print".

@SigmaPic
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I aggree that a constant can be used at line 134. But I would use (1u << (UNITY_LONG_WIDTH-1)) instead of (1l << (UNITY_LONG_WIDTH-1)) since we want an unsigned number.

Putting a signed constant result in the same issue that #76 (comment), no ?

Please sign in to comment.