Comments (6)
Ha!
This is most definitely a bug. I'll look into cleaning this function up, unless you have a suggestion already. I'm not a fan of using a larger type, because we can't guarantee that all targets will have a larger type available. I'm sure we can fix it somehow, though.
Mark
from unity.
Indeed, I have a better solution than using a 64-bit data type.
Just declaring the variable "number" as a _U_UINT instead of a _U_SINT and changing a litlle bit the sign check should do the trick.
This is my suggestion:
void UnityPrintNumber(const _U_SINT number_to_print)
{
_U_SINT divisor = 1;
_U_SINT next_divisor;
_U_UINT number;
if (number_to_print < 0)
{
UNITY_OUTPUT_CHAR('-');
number = -number_to_print;
}
else
{
number = number_to_print;
}
// figure out initial divisor
while (number / divisor > 9)
{
next_divisor = divisor * 10;
if (next_divisor > divisor)
divisor = next_divisor;
else
break;
}
// now mod and print, then divide divisor
do
{
UNITY_OUTPUT_CHAR((char)('0' + (number / divisor % 10)));
divisor /= 10;
}
while (divisor > 0);
}
from unity.
Awesome. This is a good solution. I've committed that change (e2d5e1c). I think this fixes this issue. Thanks for bringing it up (and for fixing it!)
from unity.
You're saying number is _U_UINT (unsigned) and yet you propose number = -number_to_print;
Wrong for many reasons across many compilers no?
Best,
Martyn
On 1 Sep 2014, at 21:38, SigmaPic [email protected] wrote:
_U_UINT number;
if (number_to_print < 0) { UNITY_OUTPUT_CHAR('-'); number = -number_to_print; }
from unity.
I agree that it's a little bit doubtful. However, I think that is kind of basic arithmetic is the same for all compilers.
I have only tested with MinGW. To be sure, we have to check in the ANSI C standard.
Or maybe something like that, but you have to define _U_SINT_MIN in order to be compatible to 32 and 64-bit mode.
I don't no, it's only suggestions. I let you choose the best solution.
void UnityPrintNumber(const _U_SINT number_to_print)
{
_U_SINT divisor = 1;
_U_SINT next_divisor;
_U_UINT number;
if (number_to_print == _U_SINT_MIN)
{
UNITY_OUTPUT_CHAR('-');
number = (_U_UINT)number_to_print;
}
else if (number < 0)
{
UNITY_OUTPUT_CHAR('-');
number = -number_to_print;
}
else
{
number = number_to_print;
}
// figure out initial divisor
while (number / divisor > 9)
{
next_divisor = divisor * 10;
if (next_divisor > divisor)
divisor = next_divisor;
else
break;
}
// now mod and print, then divide divisor
do
{
UNITY_OUTPUT_CHAR((char)('0' + (number / divisor % 10)));
divisor /= 10;
}
while (divisor > 0);
}
from unity.
Martyn: Good though. I'm glad you double checked this.
The ANSI C standard has the negative unary operator on the same "level" as casting for its order of operations. I think this means that the negation should happen first, which the negative of 0x80000000 is undefined according to C99 ยง7.20.6.1/2. Undefined is definitely NOT what we are looking for. ;)
Therefore, I'm updating to SigmaPic's second solution. This one appears like it should work universally. I've tweaked it slightly to explicitly cast. If you have a moment, I'd love it if you would double check it. (f480051)
Thanks again, both of you.
from unity.
Related Issues (20)
- Possibly typo in test_unity_integers_64.c and test_unity_arrays.c HOT 1
- Avoid running the setup for some specific tests HOT 3
- #529 still unfixed after merge HOT 2
- PlatformIO failing to parse manifest on install. HOT 1
- Game thing.com HOT 1
- How do I rename the `setUp` and `tearDown` functions? HOT 2
- Build fails with -Werror=missing-declarations when UNITY_USE_COMMAND_LINE_ARGS is enabled HOT 1
- XML/JSON based report generating? HOT 2
- Errr how u use this HOT 1
- Can you code on iPad using this app?
- I'm in secondary school first grade sooo i have no idea what I'm doing (I'm trying to tell but not expose my age) HOT 1
- Can you use this app to code on iPad?
- ssize_t asserts are missing
- Add Example for Testing using CMake
- Build fails when not using UNITY_USE_COMMAND_LINE_ARGS
- Feature Request: Support asymmetric range assertions HOT 2
- Some codes failed `-Werror=float-equal` compiler flag, please fix it.
- UNITY_NORETURN redefines noreturn, uses stdnoreturn.h which can break compatibility with C23 and is being defined when unnecessary HOT 1
- Generic assertion HOT 3
- Invalid file name reported HOT 18
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. ๐๐๐
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google โค๏ธ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from unity.