473,406 Members | 2,345 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

How to convert a negative value to positive

Hello

Sorry this really is a newbie question!

I am doing some floating point arithmetic and calculating the time
difference between two dates. The date values being comparied are actually
double values. I was going to compare the dates by calculating the
difference betwen the two numbers. But sometimes you get a -ve and
sometimes a +ve value. I want to disregard a small variance eg of up to two
minutes but what is best way to compare values.

ie calculate difference and then convert any -ve values to +ve then check if
(diff > x)

or some other way?

AC
Nov 14 '05 #1
2 11229
>Sorry this really is a newbie question!

I am doing some floating point arithmetic and calculating the time
difference between two dates. The date values being comparied are actually
double values. I was going to compare the dates by calculating the
difference betwen the two numbers. But sometimes you get a -ve and
sometimes a +ve value.
The unary minus operator will convert a negative value to a positive one.
I want to disregard a small variance eg of up to two
minutes but what is best way to compare values.

ie calculate difference and then convert any -ve values to +ve then check if
(diff > x)


You may want to use fabs(), in code similar to this:
#include <math.h>
double x1;
double x2;
double delta;

... put values in x1, x2, and delta ...;
if (fabs(x1-x2) > delta) { ... too far apart ... ; }
else { ... pretty close ... ; }

Gordon L. Burditt
Nov 14 '05 #2
Angus Comber wrote:
I am doing some floating point arithmetic and calculating the time
difference between two dates. The date values being comparied are actually
double values. I was going to compare the dates by calculating the
difference betwen the two numbers. But sometimes you get a -ve and
sometimes a +ve value. I want to disregard a small variance eg of up to two
minutes but what is best way to compare values.


Personally, storing dates as floating point seems
very strange to me. Yes, time is continuous(*), but not the
way it is usually measured, and not the way you want it
measured.

(*) I believe it is still unknown if quantum mechanics quantizes
time or not.

-- glen

Nov 14 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: David Levine | last post by:
Using v1.1. of the framework: I was writing a wrapper around the java.util.zip.ZipOutputStream class so that my app could access it using standard FileStream semantics and I ran into a...
11
by: Grant Edwards | last post by:
I give up, how do I make this not fail under 2.4? fcntl.ioctl(self.dev.fileno(),0xc0047a80,struct.pack("HBB",0x1c,0x00,0x00)) I get an OverflowError: long int too large to convert to int ...
2
by: Goran | last post by:
Hi! I need to convert from a unsigned char array to a float. I don't think i get the right results in the program below. unsigned char array1 = { 0xde, 0xc2, 0x44, 0x23}; //I'm not sure in...
7
by: Golan | last post by:
Hi, I need to convert a Binary value to Decimal. I've been told that the value is an unsigned one. How can I do this? I use memcpy into an unsigned char variable, but when I print the value I got...
2
by: [Gho] | last post by:
How to convert a Hexa value to negative and non negative value : if i get 0x1d = result should be 29 but if i get fd = result should be -3 How to do that
15
by: jaks.maths | last post by:
How to convert negative integer to hexadecimal or octal number? Ex: -568 What is the equivalent hexadecimal and octal number??
39
by: Frederick Gotham | last post by:
I have a general idea about how negative number systems work, but I'd appreciate some clarification if anyone would be willing to help me. Let's assume we're working with an 8-Bit signed integer,...
30
by: ceeques | last post by:
Hi I am a novice in C. Could you guys help me solve this problem - I need to convert integer(and /short) to string without using sprintf (I dont have standard libray stdio.h). for...
19
by: VK | last post by:
http://groups.google.com/group/comp.lang.javascript/browse_frm/thread/ b495b4898808fde0> is more than one month old - this may pose problem for posting over some news servers. This is why I'm...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.