Go Back   vb.org Archive > vBulletin 4 Discussion > vB4 Programming Discussions
FAQ Community Calendar Today's Posts Search

Reply
 
Thread Tools Display Modes
  #1  
Old 01-03-2010, 01:09 PM
Vaupell's Avatar
Vaupell Vaupell is offline
 
Join Date: Apr 2008
Location: Esbjerg, Denmark
Posts: 1,036
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default [php] Using the abs() function ?

Doing some calculations that may wary depending on the user, and
content calculated ofcourse..

But ewentually its bound to happen i will hit values below 0
and using integers as whole numbers no decimals

one calc could be as simple as

$result=$contentA-$ContentB

But i feel like i dont have control over it,
it might end up being 100 - 10 and will result in -90

So ive found the php manual and read about abs();
But got worried reading all the comments below
http://php.net/manual/en/function.abs.php

-----------------
What i want, is if the case is that $result ends up as a negative value i just
want to turn it positive. so in the case above the -90 would be 90.
was thinking doing something like this
PHP Code:

$result
=$contentA-$ContentB
if ($result <= 0)
 {
  
$result=$abs3($result);
 } 
Is that far fetched ? or is there a better solution ?
Reply With Quote
  #2  
Old 01-03-2010, 01:47 PM
winstone winstone is offline
 
Join Date: Dec 2006
Posts: 68
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

not sure about abs function but you can also use a simple math to achieve that
PHP Code:
$result = ($result*(-2))/2
if -6, it will become 6
if 0, it will become 0

with abs, apparently 0 turns into 1
Reply With Quote
  #3  
Old 01-03-2010, 02:07 PM
Vaupell's Avatar
Vaupell Vaupell is offline
 
Join Date: Apr 2008
Location: Esbjerg, Denmark
Posts: 1,036
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Thank you winstone.
Reply With Quote
  #4  
Old 01-03-2010, 08:05 PM
Dygear's Avatar
Dygear Dygear is offline
 
Join Date: Apr 2008
Location: Levittown, NY
Posts: 45
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

PHP Code:
$result abs($contentA $ContentB); 
Reply With Quote
  #5  
Old 01-03-2010, 09:29 PM
Vaupell's Avatar
Vaupell Vaupell is offline
 
Join Date: Apr 2008
Location: Esbjerg, Denmark
Posts: 1,036
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Quote:
Originally Posted by Dygear View Post
PHP Code:
$result abs($contentA $ContentB); 
Thank you it works, went back to abs
cause the math solution came up with some strange numbers..
Reply With Quote
  #6  
Old 01-04-2010, 02:07 AM
Dygear's Avatar
Dygear Dygear is offline
 
Join Date: Apr 2008
Location: Levittown, NY
Posts: 45
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Quote:
Originally Posted by Vaupell View Post
Thank you it works, went back to abs
cause the math solution came up with some strange numbers..
My old solution before I found out about the abs function was this:

PHP Code:
function absolute_sub($i1$i2) {
        
# Ensure Absolute Value after Subtraction.
        
if ($i1 == $i2)
                return 
0;
        else if (
$i1 $i2)
                return 
$i1 $i2;
        else
                return 
$i2 $i1;

Boy, did I feel stupid after I found out about abs.
Reply With Quote
  #7  
Old 01-04-2010, 11:02 AM
consolegaming consolegaming is offline
 
Join Date: Jan 2007
Posts: 168
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Wouldn't the non abs method just be multiply by -1 if the value is less than 0?

PHP Code:
if ($result <= 0)
 {
  
$result $result * -1;
 } 
Wouldn't that be the way to do it if abs wasn't available?
Reply With Quote
  #8  
Old 01-04-2010, 12:45 PM
Vaupell's Avatar
Vaupell Vaupell is offline
 
Join Date: Apr 2008
Location: Esbjerg, Denmark
Posts: 1,036
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Quote:
Originally Posted by consolegaming View Post
Wouldn't the non abs method just be multiply by -1 if the value is less than 0?

PHP Code:
if ($result <= 0)
 {
  
$result $result * -1;
 } 
Wouldn't that be the way to do it if abs wasn't available?
muiltiply by 1 does not do anything

-500 * 1 = -500 still..
try it on your calculator in your OS.
Reply With Quote
  #9  
Old 01-04-2010, 01:45 PM
consolegaming consolegaming is offline
 
Join Date: Jan 2007
Posts: 168
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Quote:
Originally Posted by Vaupell View Post
muiltiply by 1 does not do anything

-500 * 1 = -500 still..
try it on your calculator in your OS.
Maybe you should re-read what I posted?

-500 * -1 = 500. Hence the if statement to make sure the result is less than 1. If result is positive then it wouldn't need to do anything to it.

Though really looking back at it what I put was slightly wrong. It should be if ($result < 0) rather than <= 0 as there's no point multiplying 0 by -1.
Reply With Quote
  #10  
Old 01-04-2010, 01:55 PM
Vaupell's Avatar
Vaupell Vaupell is offline
 
Join Date: Apr 2008
Location: Esbjerg, Denmark
Posts: 1,036
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

right got it year that would work..
Reply With Quote
Reply


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT. The time now is 10:03 PM.


Powered by vBulletin® Version 3.8.12 by vBS
Copyright ©2000 - 2025, vBulletin Solutions Inc.
X vBulletin 3.8.12 by vBS Debug Information
  • Page Generation 0.04489 seconds
  • Memory Usage 2,268KB
  • Queries Executed 11 (?)
More Information
Template Usage:
  • (1)SHOWTHREAD
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_header_end
  • (1)ad_header_logo
  • (1)ad_navbar_below
  • (1)ad_showthread_beforeqr
  • (1)ad_showthread_firstpost
  • (1)ad_showthread_firstpost_sig
  • (1)ad_showthread_firstpost_start
  • (7)bbcode_php
  • (4)bbcode_quote
  • (1)footer
  • (1)forumjump
  • (1)forumrules
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (1)navbar
  • (3)navbar_link
  • (120)option
  • (1)pagenav
  • (1)pagenav_curpage
  • (1)pagenav_pagelink
  • (10)post_thanks_box
  • (10)post_thanks_button
  • (1)post_thanks_javascript
  • (1)post_thanks_navbar_search
  • (10)post_thanks_postbit_info
  • (10)postbit
  • (10)postbit_onlinestatus
  • (10)postbit_wrapper
  • (1)spacer_close
  • (1)spacer_open
  • (1)tagbit_wrapper 

Phrase Groups Available:
  • global
  • inlinemod
  • postbit
  • posting
  • reputationlevel
  • showthread
Included Files:
  • ./showthread.php
  • ./global.php
  • ./includes/init.php
  • ./includes/class_core.php
  • ./includes/config.php
  • ./includes/functions.php
  • ./includes/class_hook.php
  • ./includes/modsystem_functions.php
  • ./includes/functions_bigthree.php
  • ./includes/class_postbit.php
  • ./includes/class_bbcode.php
  • ./includes/functions_reputation.php
  • ./includes/functions_post_thanks.php 

Hooks Called:
  • init_startup
  • init_startup_session_setup_start
  • init_startup_session_setup_complete
  • cache_permissions
  • fetch_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • style_fetch
  • cache_templates
  • global_start
  • parse_templates
  • global_setup_complete
  • showthread_start
  • showthread_getinfo
  • forumjump
  • showthread_post_start
  • showthread_query_postids
  • showthread_query
  • bbcode_fetch_tags
  • bbcode_create
  • showthread_postbit_create
  • postbit_factory
  • postbit_display_start
  • post_thanks_function_post_thanks_off_start
  • post_thanks_function_post_thanks_off_end
  • post_thanks_function_fetch_thanks_start
  • post_thanks_function_fetch_thanks_end
  • post_thanks_function_thanked_already_start
  • post_thanks_function_thanked_already_end
  • fetch_musername
  • postbit_imicons
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • postbit_display_complete
  • post_thanks_function_can_thank_this_post_start
  • pagenav_page
  • pagenav_complete
  • tag_fetchbit_complete
  • forumrules
  • navbits
  • navbits_complete
  • showthread_complete