vb.org Archive

vb.org Archive (https://vborg.vbsupport.ru/index.php)
-   vB4 Programming Discussions (https://vborg.vbsupport.ru/forumdisplay.php?f=252)
-   -   [php] Using the abs() function ? (https://vborg.vbsupport.ru/showthread.php?t=232027)

Vaupell 01-03-2010 01:09 PM

[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 ?

winstone 01-03-2010 01:47 PM

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

Vaupell 01-03-2010 02:07 PM

Thank you winstone.

Dygear 01-03-2010 08:05 PM

PHP Code:

$result abs($contentA $ContentB); 


Vaupell 01-03-2010 09:29 PM

Quote:

Originally Posted by Dygear (Post 1946321)
PHP Code:

$result abs($contentA $ContentB); 


Thank you it works, went back to abs
cause the math solution came up with some strange numbers..

Dygear 01-04-2010 02:07 AM

Quote:

Originally Posted by Vaupell (Post 1946408)
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.

consolegaming 01-04-2010 11:02 AM

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?

Vaupell 01-04-2010 12:45 PM

Quote:

Originally Posted by consolegaming (Post 1946791)
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.

consolegaming 01-04-2010 01:45 PM

Quote:

Originally Posted by Vaupell (Post 1946862)
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.

Vaupell 01-04-2010 01:55 PM

right got it :D year that would work..


All times are GMT. The time now is 12:47 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.01092 seconds
  • Memory Usage 1,747KB
  • Queries Executed 10 (?)
More Information
Template Usage:
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_header_end
  • (1)ad_header_logo
  • (1)ad_navbar_below
  • (7)bbcode_php_printable
  • (4)bbcode_quote_printable
  • (1)footer
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (6)option
  • (1)pagenav
  • (1)pagenav_curpage
  • (1)pagenav_pagelink
  • (1)post_thanks_navbar_search
  • (1)printthread
  • (10)printthreadbit
  • (1)spacer_close
  • (1)spacer_open 

Phrase Groups Available:
  • global
  • postbit
  • showthread
Included Files:
  • ./printthread.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/class_bbcode_alt.php
  • ./includes/class_bbcode.php
  • ./includes/functions_bigthree.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
  • printthread_start
  • pagenav_page
  • pagenav_complete
  • bbcode_fetch_tags
  • bbcode_create
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • printthread_post
  • printthread_complete