Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8212255
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T10:41:34+00:00 2026-06-07T10:41:34+00:00

float test=-1; produces a float with value -1 . However, float test=arc4random()%500-500; produces enormous

  • 0
float test=-1;

produces a float with value -1. However,

float test=arc4random()%500-500;

produces enormous values that clearly resulted from a buffer overflow — the numbers wrapping around. This should not happen with a float; for kicks, I tried to see if Xcode would let me make a “signed float”, but it told me “floats cannot be signed or unsigned.”

I produced a work-around where I made a signed int, then cast it to a float, but I’d really appreciate knowing how/why this happened.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T10:41:36+00:00Added an answer on June 7, 2026 at 10:41 am

    arc4random() returns a u_int32_t according to the man page, so arc4random()%500-500 will be an unsigned integer, and subtracting 500 will give you a very large positive value. Try this instead:

    float test = ((float)(arc4random() % 500))-500;
    

    I produced a work-around where I made a signed int, then cast it to a
    float, but I’d really appreciate knowing how/why this happened.

    Look at the entire line one piece at a time. First, you’ve got:

    arc4random()
    

    As I said above, that returns an unsigned int. Let’s pretend that it returns the value 12345. Next, you’ve got the modulus operator, so your expression is something like:

    12345 % 500
    

    which is to say 345. Next, you subtract 500:

    345 - 500
    

    You’d think that’d give you -155, but no — we’re still working in the realm of unsigned ints here, so you really get 4294967141 (or something like that — my math may be off). Then, finally, you assign that to a float:

    float test = 4294967141;
    

    A float only has 23 bits for the mantissa, so the value that’s stored in test will be in the general neighborhood of 4294967141, but with less precision, like 4294970000.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some simple code that is comparing two float values to illustrate a
I cake-baked ( cake bake ) a test installation that resulted the Product Controller
I am trying to obtain a float value from my Database The following checks
class Test { public: SOMETHING DoIt(int a) { float FLOAT = 1.2; int INT
static void testlock() { for(int i=0;i<10000000;i++) { float f=2.0/i; } } static void TEST()
I have some HTML Code: <html> <head> <title>css test</title> <style type=text/css> .box{width:100%;float:left;background:red} </style> </head>
I want to float a element so that it stays above elements on its
A little test program: #include <iostream> const float TEST_FLOAT = 1/60; const float TEST_A
I need to test whether each number from 1 to 1000 is a multiple
I'm storing values in a column defined as float. When I read back these

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.