4
saas
6y

Can the subscript of an array be a floating point number ?
i know, i know, i can find answers on google and i found one on quora but i want you guys to help me πŸ€— in the comments

Comments
  • 5
    No, Not in an classical array. The index describes the offset into the data structure and since there are no fractional bits, this is impossible.
    If your array isn't an array but rather a map, this would be possible. But that would be a totally different data structure. I believe a language like python sees the difference automatically, so you could just use an "array" with arbitrary indexes (strings, floats, objects, whatever).
    Another Problem that emerges is that floating point numbers are not countable. How many elements does your array have if the largest index is two? How many numbers are there between 0 and 2? Infinitely many and that's definitely a problem if you want to loop over the "array".
  • 2
    Sure, if you want to get the higher nibble of the first byte and the lower nibble of the second byte, you can access them by

    unsigned char floatTestArray[2] = { 0xA0, 0x02 };

    unsigned char wtf = floatTestArray[0.5];

    printf( "WTF = %u", wtf );

    ==> WTF = 42

    Try it with [0.75] or [0.18538573]

    πŸƒ
  • 0
    @CodePatronus not as much as you do prolly 😳
  • 0
    @TobyAsE thanks dude
  • 0
    @CodePatronus πŸ˜… it's ok.
  • 0
    The teacher said the subscript can be a floating point and i argued that it cannot be. He said you'll learn it later🀦‍♂️.

    Later like after I'm dead πŸ˜‚.
  • 1
    @kayb01000010 you can cast a float to a different type (an unsigned integer) and use that (will be almost completely garbage though). You'll want to use something from the standard library to grab a rounded off integer version of the float.
  • 0
    Dictionary<float, another type> floatingPointIndexArray;
  • 1
    Absolutely fucking not
  • 1
    No. Indexes of an array indicate it's elements' placement in memory. Allocation starts at the base address, index 0, and increments by the size of the data type of the array.

    For example, an int array has size of 4 bytes(typically) for each element. The locations would be 0x00, 0x04, 0x08, 0x0c, and so on. So using a floating point number for an index wouldn't make sense. What memory address would x[1.72962] occupy?
Add Comment