View previous topic :: View next topic |
Author |
Message |
komisarios
Joined: 17 Jun 2006 Posts: 38
|
Posted: Thu Mar 21, 2019 9:20 pm Post subject: Sign reversal on Single variable? |
|
|
Hi everybody,
Does anyone know how can i change a positive value of a Single to make it negative ??
Thanks
(BASCOM-AVR version : 2.0.8.1 ) _________________ Macedonia is Greek |
|
Back to top |
|
|
theo64
Joined: 05 Jun 2005 Posts: 11
|
Posted: Thu Mar 21, 2019 9:51 pm Post subject: |
|
|
a=-a maybe? _________________ Always look for the best and expect the worst |
|
Back to top |
|
|
six1
Joined: 27 Feb 2009 Posts: 553
|
Posted: Fri Mar 22, 2019 6:31 am Post subject: |
|
|
s=10
s=s * -1
print s --> -10
to ignore sign use ABS()
s=-10
s= ABS(s)
print s --> 10 _________________ For technical reasons, the signature is on the back of this message. |
|
Back to top |
|
|
hgrueneis
Joined: 04 Apr 2009 Posts: 902 Location: A-4786 Brunnenthal
|
Posted: Sat Mar 23, 2019 9:17 am Post subject: |
|
|
A very simple way would be:
a=a-(2a)
regards
Hubert |
|
Back to top |
|
|
MWS
Joined: 22 Aug 2009 Posts: 2262
|
Posted: Sat Mar 23, 2019 4:45 pm Post subject: |
|
|
As a basic rule multiplications and divisions should be avoided, where additions and subtractions are usable, as the latter are faster:
Not much gain in this case, 'only' 28 percent faster.
Where speed is required, 28 percent is really something. |
|
Back to top |
|
|
theo64
Joined: 05 Jun 2005 Posts: 11
|
Posted: Sun Mar 24, 2019 3:16 pm Post subject: |
|
|
which i think is the same as i suggested.. a = -a |
|
Back to top |
|
|
Netzman
Joined: 25 Nov 2005 Posts: 127 Location: Graz
|
Posted: Sun Mar 24, 2019 3:30 pm Post subject: |
|
|
Possibly the fastest method:
Code: | Dim Number As Single
Dim Upperbyte As Byte At Number + 3 Overlay
Const Signreverse = &H80
Number = 1
Upperbyte = Upperbyte Xor Signreverse ' 8 cycles
Print Number |
Takes 8 cycles.
0 - s takes 202, -s and -1 * s each complete in 279 cycles. _________________ LCD Menu | Proportional Fonts |
|
Back to top |
|
|
MWS
Joined: 22 Aug 2009 Posts: 2262
|
|
Back to top |
|
|
Evert :-)
Joined: 18 Feb 2005 Posts: 2156
|
|
Back to top |
|
|
hgrueneis
Joined: 04 Apr 2009 Posts: 902 Location: A-4786 Brunnenthal
|
Posted: Sun Mar 24, 2019 5:42 pm Post subject: |
|
|
as I implied before.
b=a+a
a=a-b
no multiplication
a*2 with binary it can also be done with shift to make it fast.
……….a - a=0!
regards
Hubert |
|
Back to top |
|
|
|