Recent

Author Topic: Looking for better way...  (Read 926 times)

OC DelGuy

  • Full Member
  • ***
  • Posts: 174
Looking for better way...
« on: February 11, 2025, 11:32:37 pm »
Is there a better way to do something like this?
I feel there should be a way to shorten the code, and avoid adding a bunch of zeros.
I want to make the case statement capable of larger numbers without having to type in all those zeros.
Are there different ways to represent numbers in the actual code that makes it less bulky?
Free Pascal Lazarus Version #: 2.2.4
Date: 24 SEP 2022
FPC Version: 3.2.2
Revision: Lazarus_2_2_4
x86_64-win64-win32/win64

ASerge

  • Hero Member
  • *****
  • Posts: 2389
Re: Looking for better way...
« Reply #1 on: February 12, 2025, 06:25:50 am »
Is there a better way to do something like this?
May be
Code: Pascal  [Select][+][-]
  1. Index := Trunc(Log10(Dec * Power(10, Exp)))

Khrys

  • Full Member
  • ***
  • Posts: 177
Re: Looking for better way...
« Reply #2 on: February 12, 2025, 06:51:45 am »
What exactly are you trying to achieve?
What should  Worded.Caption  represent in terms of  Dec  and  Exp?

ASerge

  • Hero Member
  • *****
  • Posts: 2389
Re: Looking for better way...
« Reply #3 on: February 12, 2025, 07:42:38 am »
What exactly are you trying to achieve?
What should  Worded.Caption  represent in terms of  Dec  and  Exp?
Show all the code to see what you are doing.

alpine

  • Hero Member
  • *****
  • Posts: 1372
Re: Looking for better way...
« Reply #4 on: February 12, 2025, 08:43:08 am »
Is there a better way to do something like this?
May be
Code: Pascal  [Select][+][-]
  1. Index := Trunc(Log10(Dec * Power(10, Exp)))
+1    :D :D

Or is it just
Code: Pascal  [Select][+][-]
  1. Exp + Log10(Dec)
?

Edit: + is for a log() decomposition of a product
« Last Edit: February 12, 2025, 09:26:20 am by alpine »
"I'm sorry Dave, I'm afraid I can't do that."
—HAL 9000

Zvoni

  • Hero Member
  • *****
  • Posts: 2897
Re: Looking for better way...
« Reply #5 on: February 12, 2025, 08:51:02 am »
What exactly are you trying to achieve?
What should  Worded.Caption  represent in terms of  Dec  and  Exp?
Show all the code to see what you are doing.
Yep.
Wouldn't be surprised if the Case-Statement could be eliminated with a simple Lookup-Array
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

alpine

  • Hero Member
  • *****
  • Posts: 1372
Re: Looking for better way...
« Reply #6 on: February 12, 2025, 09:33:09 am »
Wouldn't be surprised if the Case-Statement could be eliminated with a simple Lookup-Array
No kidding!
As ASerge wrote Log10 will give you simply the number of zeros. But that so blindly: simple math rule: log(x*y) = log(x) + log(y), which is log10(Dec) + log10(power(10, Exp)).
https://www.onlinemathlearning.com/logarithms-product-rule.html 
"I'm sorry Dave, I'm afraid I can't do that."
—HAL 9000

Khrys

  • Full Member
  • ***
  • Posts: 177
Re: Looking for better way...
« Reply #7 on: February 12, 2025, 01:09:37 pm »
As ASerge wrote Log10 will give you simply the number of zeros. But that so blindly: simple math rule: log(x*y) = log(x) + log(y), which is log10(Dec) + log10(power(10, Exp)).
https://www.onlinemathlearning.com/logarithms-product-rule.html

And don't forget that  Log10(Power(10, Exp)) = Exp   ;)

alpine

  • Hero Member
  • *****
  • Posts: 1372
Re: Looking for better way...
« Reply #8 on: February 12, 2025, 01:21:23 pm »
And don't forget that  Log10(Power(10, Exp)) = Exp   ;)
Yup, that's what I meant in post #4.
"I'm sorry Dave, I'm afraid I can't do that."
—HAL 9000

Khrys

  • Full Member
  • ***
  • Posts: 177
Re: Looking for better way...
« Reply #9 on: February 12, 2025, 04:26:35 pm »
And don't forget that  Log10(Power(10, Exp)) = Exp   ;)
Yup, that's what I meant in post #4.

Oops, I must've missed that   :-[
Gotta be more careful when reading or I'll end up another Thaddy   :D

 

TinyPortal © 2005-2018