Author Topic: How do I select each individual digit of a 5 digit floating point number?  (Read 1015 times)

djsb

  • Full Member
  • ***
  • Posts: 122
    • View Profile
I asked the same question of BARD AI (although not in exactly the same way). Here are some of the responses.

Code: [Select]
#include <stdio.h>

int main() {
  float number = 87.50;

  while (number <= 108.00) {
    // Get the least significant digit of the number.
    int least_significant_digit = number % 10;

    // Print the least significant digit.
    printf("%d ", least_significant_digit);

    // Increment the number.
    number += 0.01;
  }

  printf("\n");

  return 0;
}

/*

// Print out NEXT LEAST significant digit.

#include <stdio.h>

int main() {
  float number = 87.50;

  while (number <= 108.00) {
    // Get the next least significant digit of the number.
    int next_least_significant_digit = (number * 10) % 100;

    // Print the next least significant digit.
    printf("%d ", next_least_significant_digit);

    // Increment the number.
    number += 0.01;
  }

  printf("\n");

  return 0;
}
*/

/*

// Print out the NEXT MOST significant digit.

#include <stdio.h>

int main() {
  float number = 87.50;

  while (number <= 108.00) {
    // Get the next most significant digit of the number.
    int next_most_significant_digit = (int)number / 10;

    // Print the next most significant digit.
    printf("%d ", next_most_significant_digit);

    // Increment the number.
    number += 0.01;
  }

  printf("\n");

  return 0;
}

*/

/*

// Print out the MOST significant digit ONCE.

#include <stdio.h>

int main() {
  float number = 87.50;
  int most_significant_digit = 0;

  while (number <= 108.00) {
    // Get the most significant digit of the number.
    most_significant_digit = get_most_significant_digit(number);

    // If the most significant digit is different from the previous most significant digit, print it.
    if (most_significant_digit != previous_most_significant_digit) {
      printf("%d ", most_significant_digit);
    }

    // Increment the number.
    number += 0.01;
  }

  printf("\n");

  return 0;
}

int get_most_significant_digit(float number) {
  """Gets the most significant digit of a number."""
  if number < 0:
    number = -number

  while number >= 10:
    number //= 10

  return number;
}

*/


I have just tried the first one in the list (try to find the LEAST significant digit of a number that is increasing from 87.50 to 108.00). The problem with the code suggested by BARD is that the modulo operator does NOT work with floating point numbers. Is there a way of separating the fractional part of the floating point number from the integer part? Then maybe they could be acted on separately. I'd like to find a solution in BCX so that I can fit it into the rest of the code i'm working on. Any clues? Thanks.


PS, I know that this should be obvious to someone who works with C (or BCX) on a daily basis. I don't and it's been a while. But like I always say I'm willing to try and dont mind washing my dirty linen in public so to say.
« Last Edit: September 13, 2023, 05:51:57 AM by djsb »

jbk

  • Sr. Member
  • ****
  • Posts: 264
    • View Profile
Re: How do I select each individual digit of a 5 digit floating point number?
« Reply #1 on: September 13, 2023, 07:11:54 AM »
djsb, in BCX you have the FRAC function
but I think that you need to narrow down the specification of your problem/goal, for example, what is the range of the values that you will be dealing with?
both positive and negative?
depending on what you want to do with the digits, you may want to convert the number to string and deal with the string to extract the digits

djsb

  • Full Member
  • ***
  • Posts: 122
    • View Profile
Re: How do I select each individual digit of a 5 digit floating point number?
« Reply #2 on: September 13, 2023, 07:32:28 AM »
I'm only dealing with 87.50 to 108.00 inclusive. I don't need to handle negative numbers or any numbers outside of this range. I'm going to be incrementing the numbers (and maybe decrementing them) in steps of 0.01. So 87.50 then 87.51 etc up to 108.00. If I can somehow convert the floating point number into a string first and then convert it back afterwards, then that's OK. Any loss of precision is not that critical as long as it's very small. I'll have a look at some console examples in the help. Thanks.

jbk

  • Sr. Member
  • ****
  • Posts: 264
    • View Profile
Re: How do I select each individual digit of a 5 digit floating point number?
« Reply #3 on: September 13, 2023, 08:10:25 AM »
the following will print the digits from the least significant to the most significant digit, for example 108.00 will be printed as 0,0,8,0,1
Code: [Select]
dim as string s, digit
dim as double xmin, xmax, dx, y
dim as long length, i

xmin=87.5#
xmax=108#
dx=.01#

for y=xmin to xmax+dx step dx
    s=using$("######.##", y)
    'print "the digits from the least to the most significant are"
    length=len(s)
    for i=length to 1 step -1
        digit=mid$(s, i, 1)
        if digit<>"." then
            print digit;
            if i>1 then
                print ",";
            else
                print
            end if
        end if
    next i
next y

pause
« Last Edit: September 13, 2023, 08:32:11 AM by jbk »

MrBcx

  • Administrator
  • Hero Member
  • *****
  • Posts: 2304
    • View Profile
Re: How do I select each individual digit of a 5 digit floating point number?
« Reply #4 on: September 13, 2023, 08:35:02 AM »
FYI:

in BCX, you use: MOD(Dividend/Divisor).  BCX translates that to fmod(Dividend/Divisor)

Read for more:  https://bcxbasiccoders.com/webhelp/html/mathfunctions.htm#mod

djsb

  • Full Member
  • ***
  • Posts: 122
    • View Profile
Re: How do I select each individual digit of a 5 digit floating point number?
« Reply #5 on: September 13, 2023, 03:51:47 PM »
Thank you for your help  :)